VBA - If Then to Loop through Pivot Items Problem

hillock

New Member
Joined
Jan 14, 2015
Messages
5
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:

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.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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