搜索数据透视表以显示详细信息(如果未找到)创建新工作表

时间:2014-08-18 16:40:46

标签: excel vba excel-vba

我需要一个VBA代码,它将在新页面上打开某个Pivot字段的详细信息,或者如果在数据透视表中找不到该字段,则会创建一个新的工作表,该工作表将被命名为该字段的假设被命名。我一直得到编译错误:如果没有阻止结束,如果我错过了什么?它似乎也完全忽略了pivot字段的显示细节,只是创建了一个没有任何内容的新工作表。非常感谢帮助这是我到目前为止所做的:

Sub
Sheets("Sheet4").Select
Columns("A:A").Select
Set Found = Cells.Find(What:="+ Deposit", After:=Cells(1, 1), LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext)
If A = Found.Address Then
Range(A).Select
ActiveCell.Offset(0, 3).Select
Selection.ShowDetail = True
Sheets("Sheet5").Name = "Deposits"
Else: Sheets.Add After:=Sheets(Sheets.Count)
Sheets("Sheet5").Name = "Deposits"
End If

Sheets("Sheet4").Select
Columns("A:A").Select
Set Found = Cells.Find(What:="- Withdrawal", After:=Cells(1, 1), LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext)
If A = Found.Address Then
Range(A).Select
ActiveCell.Offset(0, 3).Select
Selection.ShowDetail = True
Sheets("Sheet6").Name = "Withdrawals"
Else: Sheets.Add After:=Sheets(Sheets.Count)
Sheets("Sheet6").Name = "Withdrawals"
End If

Sheets("Sheet4").Select
Columns("A:A").Select
Set Found = Cells.Find(What:="- Check", After:=Cells(1, 1), LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext)
If A = Found.Address Then
Range(A).Select
ActiveCell.Offset(0, 3).Select
Selection.ShowDetail = True
Sheets("Sheet7").Name = "Checks"
Else: Sheets.Add After:=Sheets(Sheets.Count)
Sheets("Sheet7").Name = "Checks"
End If

EndSub

1 个答案:

答案 0 :(得分:0)

语法需要更像这样:

Sub subName()

Sheets("Sheet4").Select
Columns("A:A").Select
Set Found = Cells.Find(What:="+ Deposit", After:=Cells(1, 1), LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext)
If A = Found.Address Then
    Range(A).Select
    ActiveCell.Offset(0, 3).Select
    Selection.ShowDetail = True
    Sheets("Sheet5").Name = "Deposits"
Else
    Sheets.Add After:=Sheets(Sheets.Count)
    Sheets("Sheet5").Name = "Deposits"
End If

Sheets("Sheet4").Select
Columns("A:A").Select
Set Found = Cells.Find(What:="- Withdrawal", After:=Cells(1, 1), LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext)
If A = Found.Address Then
    Range(A).Select
    ActiveCell.Offset(0, 3).Select
    Selection.ShowDetail = True
    Sheets("Sheet6").Name = "Withdrawals"
Else
    Sheets.Add After:=Sheets(Sheets.Count)
    Sheets("Sheet6").Name = "Withdrawals"
End If

Sheets("Sheet4").Select
Columns("A:A").Select
Set Found = Cells.Find(What:="- Check", After:=Cells(1, 1), LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext)
If A = Found.Address Then
    Range(A).Select
    ActiveCell.Offset(0, 3).Select
    Selection.ShowDetail = True
    Sheets("Sheet7").Name = "Checks"
Else
    Sheets.Add After:=Sheets(Sheets.Count)
    Sheets("Sheet7").Name = "Checks"
End If

End Sub

但你并没有具体说明它是什么" A"是。我假设您正在寻找一些具体的价值,并将替换您的身边进行测试。

需要注意的一点是,宏录制器并不能捕获Excel的所有功能。像Freeze Panes这样的东西往往只是以非常特殊的方式表现出来,我想透视表可能与展示细节不友好。你可能会尝试直接指定单元格而不是先选择它。换句话说:

On Error Resume Next
foundRow = Cells.Find(What:="+ Deposit", After:=Cells(1, 1), LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext).Row
On Error Goto 0
If foundRow > 1 Then
    Cells(foundRow, 3).ShowDetail = True
    '...assuming "ShowDetail" created a new sheet
    ActiveSheet.Name = "Deposits"
Else