而不是使用 For Each 循环,将 FilterArr 更改为枢轴引用,然后您可以使用将值设置.VisibleItemsList
为您的 FilterArr。这是一个例子。
FilterArr = Array( _
"[myTableName].[myPivotField].&[myPivotItem1]", _
"[myTableName].[myPivotField].&[myPivotItem2]")
ActiveSheet.PivotTables("PivotTable2").PivotFields( _
"[myTableName].[myPivotField].[myPivotField]").VisibleItemsList = FilterArr
此示例假定您在同一个工作簿中有一个工作表,其中一个表包含应过滤数据透视表的值。每个过滤器值的透视过滤器字符串使用相邻列中的公式创建,然后读入数组。使用命名范围中的值创建其他变量。
Sub FilterPivot_WithListOfValues()
'---------------------------------------------------------------------------------------------------------
' Purpose: Dynamically create an array based on the values of one column of a table.
'
' Customize: Inputs in this sub come from 6 named ranges on a worksheet in this workbook.
' myPivotTableName, myPivotFieldName, mySheetName,
' myFilterTableName, smyDataTableName, myFilterCol
'
' Revisions:
' 09/30/19 Sub created
'
'---------------------------------------------------------------------------------------------------------
'~~~> Set the data types for the variables.
Dim oPT As PivotTable
Dim oPF As PivotField
Dim oPI As PivotItem
Dim strPT As String 'pivot table name
Dim strPF As String 'pivot field name
Dim strWS As String 'pivot table sheet name
Dim oWS As Worksheet
Dim oFTable As ListObject
Dim oDTable As ListObject
Dim strFTable As String 'filter table name
Dim strDTable As String 'data table name
Dim strFilterString As String 'pivot table filter string
Dim FilterArr() As Variant
Dim TempArray
Dim i As Long
Dim iCol As Long
'~~~> Assign the variables.
strPT = [myPivotTableName]
strPF = [myPivotFieldName]
strWS = [mySheetName]
strFTable = [myFilterTableName]
strDTable = [myDataTableName]
iCol = [myFilterCol]
'~~~> Build the filter string. Don't include the quotation marks wrapper.
' That is automatically added by .PivotFields
strFilterString = "[" & strDTable & "].[" & strPF & "].[" & strPF & "]"
'~~~> Check the variables in the Immediate Window.
Debug.Print "Variable Set: strPT = " & strPT
Debug.Print "Variable Set: strPF = " & strPF
Debug.Print "Variable Set: strWS = " & strWS
Debug.Print "Variable Set: strDTable = " & strDTable
Debug.Print "Variable Set: strFTable = " & strFTable
Debug.Print "Variable Set: iCol = " & iCol
Debug.Print "Variable Set: strFilterString = " & strFilterString
'~~~> Set the path for the table variable
Set oFTable = Range(strFTable).ListObject
'~~~> Set the path for the sheet variable
Set oWS = Worksheets(strWS)
'~~~> Create an array list from a table column.
TempArray = oFTable.DataBodyRange.Columns(iCol)
'~~~> Convert from vertical array to horizontal array list.
FilterArr = Application.Transpose(TempArray)
DisplayArrayValues:
'~~~> Loop through each item in the table array and display in Immediate Window [ctrl + g]
For i = LBound(FilterArr) To UBound(FilterArr)
Debug.Print FilterArr(i)
Next i
ApplyPivotFilters:
'~~~> Apply filters to the pivot table.
Set oPF = oWS.PivotTables(strPT).PivotFields(strFilterString)
With oPF
.VisibleItemsList = FilterArr
End With
ReleaseVariables:
'~~~> Release the variables from memory.
Set oPT = Nothing
Set oPF = Nothing
Set oPI = Nothing
Set oWS = Nothing
Set oFTable = Nothing
Set oDTable = Nothing
strPT = vbNullString
strPF = vbNullString
strWS = vbNullString
strFTable = vbNullString
strDTable = vbNullString
strFilterString = vbNullString
i = vbNull
iCol = vbNull
End Sub
这是过滤器表和命名范围的片段。
这是用于生成过滤器字符串的公式。
="["&myDataTableName&"].["&myPivotFieldName&"].&["&[@[Pivot Items]]&"]"