我是数组的新手,以及如何正确使用它们。我不断收到MyArray(i,1) = subscript out of range
,它突出显示了行,并在其中放置了这些星号。
我想最小化行数并提高效率,因此我可以将同一数组拉到不同的枢轴进行过滤。
Sub Macro1()
Dim MyArray() As Variant
Dim i As Integer
'Populate the array.
MyArray = Array("I1", "I2", "I3")
'Filter based off array values
For i = 1 To UBound(MyArray)
* ActiveSheet.PivotTables("PivotTable1").PivotFields("letters "). _
* CurrentPage = MyArray(i, 1)
Next i
End Sub
我想提高效率的旧代码是:
Sub Macro1()
Sheets("NonDomestic").PivotTables("PivotTable1").PivotFields("letters "). _
CurrentPage = "(All)"
With Sheets("NonDomestic").PivotTables("PivotTable1").PivotFields( _
"dir sales ship cust cot ")
.ClearAllFilters
.PivotItems("A1").Visible = False
.PivotItems("B1").Visible = False
.PivotItems("C1").Visible = False
.PivotItems("C2").Visible = False
.PivotItems("D1").Visible = False
.PivotItems("D2").Visible = False
.PivotItems("D3").Visible = False
.PivotItems("D4").Visible = False
.PivotItems("D5").Visible = False
.PivotItems("D6").Visible = False
.PivotItems("D7").Visible = False
.PivotItems("E1").Visible = False
.PivotItems("F1").Visible = False
.PivotItems("F2").Visible = False
.PivotItems("F3").Visible = False
.PivotItems("F4").Visible = False
.PivotItems("F5").Visible = False
.PivotItems("F6").Visible = False
.PivotItems("F7").Visible = False
.PivotItems("G1").Visible = False
.PivotItems("G2").Visible = False
.PivotItems("G3").Visible = False
.PivotItems("G4").Visible = False
.PivotItems("G5").Visible = False
.PivotItems("H1").Visible = False
.PivotItems("H3").Visible = False
.PivotItems("H4").Visible = False
'.PivotItems("I1").Visible = False
'.PivotItems("I2").Visible = False
'.PivotItems("I3").Visible = False
End With
On Error GoTo 0
答案 0 :(得分:0)
CurrentPage
仅用于显示过滤器中的1 PivotItem
。
如果您有可见/不可见的PivotItems
列表,则将它们分别设置为可见,并按其名称或索引对其进行寻址。
Private Sub PivotFilterTest()
Dim pf As PivotField
Dim myArray() As Variant
Dim i As Long
myArray = Array("I1", "I2", "I3")
Set pf = ActiveSheet.PivotTables("PivotTable1").PivotFields("letters ")
With pf
.ClearManualFilter
.EnableMultiplePageItems = True
For i = LBound(myArray) To UBound(myArray)
.PivotItems(myArray(i)).Visible = False
Next i
End With
End Sub