我正在尝试设置数据透视表字段visible = false
中的所有项目。例如,我在每个国家有5个部门,分别是DivA,DivB,DivC,DivD和DivE。现在有时在我的源数据中,我有比上面提到的分区更多或更少,所以我想在枢轴场“分区”下关闭所有的otions,然后如果它们在那里可以看到所有上面提到的分区(有时候可能有3,4个或全部5个部门。
我在网上发现了一个代码并尝试合并它,但它一直给我设置“False = False”的错误。
非常感谢任何帮助!
请参阅下面的代码:
Sub test()
'
' test Macro
'
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Division")
Dim Table As PivotTable
Dim FoundCell As Object
Dim All As Range
Dim PvI As PivotItem
Set All = Worksheets("Sheet1").Range("A7:AZ10000")
Set Table = Worksheets("Sheet1").PivotTables("PivotTable3")
For Each PvI In Table.PivotFields("Division").PivotItems
Set FoundCell = All.Find(PvI.Name)
If FoundCell <> "itemname" Then
PvI.Visible = False
End If
Next
.PivotItems("DivA").Visible = True
.PivotItems("DivB").Visible = True
.PivotItems("DivC").Visible = True
.PivotItems("DivD").Visible = True
.PivotItems("DivE").Visible = True
End With
End Sub
答案 0 :(得分:2)
这是你在尝试什么? (的 UNTESTED 强>)
Sub test()
Dim table As PivotTable
Dim PvI As PivotItem
Set table = Worksheets("Sheet1").PivotTables("PivotTable3")
With table.PivotFields("Division")
For Each PvI In .PivotItems
Select Case PvI.Name
Case "DivA", "DivB", "DivC", "DivD", "DivE"
PvI.Visible = True
Case Else
PvI.Visible = False
End Select
Next
End With
End Sub
答案 1 :(得分:0)
我测试了答案宏...它有效。
Sub Pivot_Test()
Sheets("Macro").Select
Dim Dt As String
Dt = Range("C1")
Dim PMth1 As String
Dim PMth2 As String
Dim PMth3 As String
Dim PMth4 As String
Dim PMth5 As String
Dim PMth6 As String
Dim PMth7 As String
Dim PMth8 As String
Dim PMth9 As String
Dim PMth10 As String
Dim PMth11 As String
Dim PMth12 As String
PMth1 = Range("J1")
PMth2 = Range("J2")
PMth3 = Range("J3")
PMth4 = Range("J4")
PMth5 = Range("J5")
PMth6 = Range("J6")
PMth7 = Range("J7")
PMth8 = Range("J8")
PMth9 = Range("J9")
PMth10 = Range("J10")
PMth11 = Range("J11")
PMth12 = Range("J12")
Sheets("MANCO").Select
Dim pt1 As PivotTable
Dim pt2 As PivotTable
Dim PI1 As PivotItem
Dim PI2 As PivotItem
Set pt1 = Worksheets("MANCO").PivotTables("Resolution")
With pt1.PivotFields("Month")
For Each PI1 In .PivotItems
Select Case PvI.Name
Case PMth1, PMth2, PMth3, PMth4, PMth5, PMth6, PMth7, PMth8, PMth9, PMth10, PMth11, PMth12
PI1.Visible = True
Case Else
PI1.Visible = False
End Select
Next
End With
Set pt2 = Worksheets("MANCO").PivotTables("Complaints")
With pt2.PivotFields("Month")
For Each PI2 In .PivotItems
Select Case PI2.Name
Case PMth1, PMth2, PMth3, PMth4, PMth5, PMth6, PMth7, PMth8, PMth9, PMth10, PMth11, PMth12
PI2.Visible = True
Case Else
PI2.Visible = False
End Select
Next
End With
End Sub