0

I have a list of random items that I want to filter in an Excel pivot table. The pivot table is connected to an external data source. Is there any way I can filter my entire list in the pivot table without adding one item at a time to the filter?

I've tried below code in VBA, which works on a local data source, but not when it's external.

"56607016", "84000110", "8A20371" are some examples of items from my list.

If there is a better solution than VBA here, please tell me.


Sub FilterPivotItems()

Dim PT          As PivotTable
Dim PTItm       As PivotItem
Dim FiterArr()  As Variant

' use an array to select the items in the pivot filter you want to keep visible
FiterArr = Array("56607016", "84000110", "8A20371")

' set the Pivot Table
Set PT = ActiveSheet.PivotTables("PivotTable2")

' loop through all Pivot Items in "Product number" Pivot field
For Each PTItm In PT.PivotFields("[Released products].[Product number].[Product number]").PivotItems
    If Not IsError(Application.Match(PTItm.Caption, FiterArr, 0)) Then ' check if current item is not in the filter array
        PTItm.Visible = True
    Else
        PTItm.Visible = False
    End If
Next PTItm

End Sub

The error message I get when running:

"Run-time error '1004': Unable to get the _NewEnum property of the PivotItems class"

A. Nadjar
  • 2,440
  • 2
  • 19
  • 20
erikperik
  • 23
  • 4

1 Answers1

0

Instead of using For Each loop, change the FilterArr to pivot reference and then you can use set the value of the .VisibleItemsList to your FilterArr. Here is an example.

FilterArr = Array( _
        "[myTableName].[myPivotField].&[myPivotItem1]", _
        "[myTableName].[myPivotField].&[myPivotItem2]")


    ActiveSheet.PivotTables("PivotTable2").PivotFields( _
        "[myTableName].[myPivotField].[myPivotField]").VisibleItemsList = FilterArr

This example assumes that you have a worksheet in the same workbook, with a table that contains the values for which the pivot table should be filtered. The pivot filter string for each filter value is created with a formula in the adjacent column and then read into an array. Additional variables are created with the values from named ranges.

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

This is a snip of the filter table and named ranges.

enter image description here

This is the formula that is used to generate the filter string.

="["&myDataTableName&"].["&myPivotFieldName&"].&["&[@[Pivot Items]]&"]"
Jenn
  • 612
  • 1
  • 4
  • 7