Hi all, I'm stuck on this issue and cannot figure out what it is I am doing wrong. I'm in the process of writing a macro to loop through visible fields in a pivot table, find the items in those fields that are filtered, and then apply those filters to the source data table. The particular problem I'm having deals with a loop that is supposed to look in a field, find the visible pivot items, and then store those items as variables. I've been messing around with trying to terminate the loop if all pivot items are visible. Below is what I have so far:
So whenever I have a filter on a city, or group of cities, the macro pastes them to a range in the spreadsheet. Eventually, I want to store them as variables, but for now I'm just pasting them for the purposes of working through the code. You can also see that when the number of visible items is equal to the total number of pivot items in that field, I'm returning a message box. Eventually, that message box will go away and be replaced by code that tells it to start looking through pivot items in the next visible field.
I realize that this might be unnecessary, as applying all visible items as filters to the source data field would result in all items being shown, which accomplishes the same thing. However, I'm more curious about what it is I'm doing wrong at this point. The code pastes all items to the range, regardless of whether there is a filter or not. However, the message box only returns when there is no filter applied, so it appears to be working correctly.
Any input/feedback would be appreciated. Thanks.
Code:
Sub PivotTest2()
Dim pi As PivotItem
Dim count As Integer
Dim available As Integer
Dim row As Integer
available = ActiveSheet.PivotTables("Cities").PivotFields("City").PivotItems.count
row = 2 'This is your starting row for display
For Each pi In ActiveSheet.PivotTables("Cities").PivotFields("City").PivotItems
If pi.Visible Then
count = count + 1
End If
If count = available Then
MsgBox ("All Pivot Items Are Selected")
End If
If count <> available Then
row = row + 1
Range("'Sheet4'!G" & row) = pi.Name
End If
Next pi
End Sub
So whenever I have a filter on a city, or group of cities, the macro pastes them to a range in the spreadsheet. Eventually, I want to store them as variables, but for now I'm just pasting them for the purposes of working through the code. You can also see that when the number of visible items is equal to the total number of pivot items in that field, I'm returning a message box. Eventually, that message box will go away and be replaced by code that tells it to start looking through pivot items in the next visible field.
I realize that this might be unnecessary, as applying all visible items as filters to the source data field would result in all items being shown, which accomplishes the same thing. However, I'm more curious about what it is I'm doing wrong at this point. The code pastes all items to the range, regardless of whether there is a filter or not. However, the message box only returns when there is no filter applied, so it appears to be working correctly.
Any input/feedback would be appreciated. Thanks.