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.
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.)
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!
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!