Looping through Pivot Fields and Applying Filters to Source Data

hillock

New Member
Joined
Jan 14, 2015
Messages
5
I've got this problem with a piece of code that I've cobbled together from various sources, and I was wondering if anyone can help me solve the last road block I'm facing. At least until another one pops up. :laugh:

Below is the code. Basically, what it's doing is going through each visible field in a pivot table, pulling the visible fields, and applying them to the source data. The only part that doesn't work, to my knowledge, is when it goes to apply the visible Pivot Items (xvarFilter) to the source data. (Sheet4 contains the pivot table, and Sheet1 is the source table.)

Code:
[U]Dim fld As Integer[/U]_____________________________________________________________________________________________________________________________________________
Sub ApplyFilter()
Dim pt As PivotTable
Dim varFilter
Dim xvarFilter
Dim i As String

For fld = 1 To ActiveSheet.PivotTables("Cities").PivotFields.count              'For all fields 'fld' in the pivot table (1 to [count of fields])
    x = ActiveSheet.PivotTables("Cities").PivotFields(fld).Orientation          'Variable x is equal to the orientation (returns boolean value) of field 'fld'
    If x > 0 Then                                                               'If the field is visible in the pivot, it will have a value > 0
        Sheet4.Activate                                                         'Activate the pivot table worksheet
        Set pt = ActiveSheet.PivotTables(1)                                     'Sets the pivot table that we want to look at
        varFilter = GetItemList(pt.PivotFields(fld))                            'Declares the variable varFilter as the array of visible items in field 'fld'
        xvarFilter = (Chr(34) & Replace(varFilter, ",", Chr(34) & ", " & _
        Chr(34)) & Chr(34))                                                     'Modifies the varFilter so that it is displayed as "Result1", "Result2",...etc.
        Sheet1.Activate                                                         'Activates Sheet1 so that the autofilter can be applied
        ActiveSheet.UsedRange.AutoFilter Field:=fld, Criteria1:=Array( _
            xvarFilter), Operator:=xlFilterValues                               'Autofilters on field 'fld', declares the Criteria to be xvarFilter
        Sheet4.Activate                                                         'Activates Sheet4 so the next field can be looped through
        End If                                                                  'Ends the if statement
Next fld                                                                        'Begins the loop again

[U]End Sub                   ______________________________________________________________________________________________________________________________________
[/U]Function GetItemList(pf As PivotField) As String
   Dim pi                As PivotItem
   Dim strOut            As String
   For Each pi In pf.VisibleItems
      strOut = strOut & "," & pi.Caption
   Next pi
   GetItemList = Mid$(strOut, 2)
End Function

Even though I've written the code (xvarFilter) to display the cities exactly as they would appear when I use the Macro recorder (i.e. "New York", "Chicago", "Los Angeles"), they do not get applied correctly, and when I go to Sheet1, no items are shown. Any help would be greatly appreciated. I'm not able to attach items, or I would provide the original spreadsheet as well. Thanks!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Forum statistics

Threads
1,223,268
Messages
6,171,100
Members
452,379
Latest member
IainTru

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top