打开选定的数据透视表详细信息或创建新工作表

时间:2014-08-18 15:10:27

标签: excel vba excel-vba

我需要一个VBA代码,它将在新页面上打开某个Pivot字段的详细信息,或者如果在数据透视表中找不到该字段,则会创建一个新工作表,该工作表将被命名为该字段应该命名的内容。我一直得到编译错误:如果没有阻止结束,如果我错过了什么?这就是我到目前为止所做的:

Sub Macro6
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

2 个答案:

答案 0 :(得分:0)

  1. 从数据透视表中选择所需的单元格。
  2. 使用ShowDetail显示选定的单元格。这将创建新的工作表。
  3. 使用所需名称重命名新创建的工作表。 (新工作表将被激活为工作表)

    Cells(TargetRow, TargetColumn).Select
    Selection.ShowDetail = True
    ActiveSheet.Name = "YourNew Sheet Name"
    

答案 1 :(得分:-1)

Sub Macro10()
'
' Macro10 Macro
'
Sheets("Sheet4").Select
Range("A4").Select
Set Found = Cells.Find(What:="+ Deposit", After:=Cells(1, 1), LookAt:=xlPart,    SearchOrder:=xlByColumns, SearchDirection:=xlNext)
On Error GoTo NoDeposit
A = Found.Address
Range(A).Select
ActiveCell.Offset(0, 3).Select
Selection.ShowDetail = True


Withdrawals:
Sheets("Sheet4").Select
Range("A4").Select
Set Found = Cells.Find(What:="- Withdrawal", After:=Cells(1, 1), LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext)
On Error GoTo NoWithdrawal
A = Found.Address
Range(A).Select
ActiveCell.Offset(0, 3).Select
Selection.ShowDetail = True


Checks:   
Sheets("Sheet4").Select
Range("A4").Select
Set Found = Cells.Find(What:="- Check", After:=Cells(1, 1), LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext)
On Error GoTo NoCheck
A = Found.Address
Range(A).Select
ActiveCell.Offset(0, 3).Select
Selection.ShowDetail = True


Labels:

Sheets("Sheet5").Name = "Deposits"
Sheets("Sheet6").Name = "Withdrawals"
Sheets("Sheet7").Name = "Checks"




Exit Sub
NoDeposit: 
Sheets.Add After:=Sheets(Sheets.Count)
Resume Withdrawals:

NoWithdrawal: Sheets.Add After:=Sheets(Sheets.Count)
Resume Checks:

NoCheck: Sheets.Add After:=Sheets(Sheets.Count)
Resume Labels:


End Sub