Loop through pivot table - VBA

DutchBaron

New Member
Joined
Jul 27, 2012
Messages
6
Hi guys,

I've got a problem.

I'm working with a database and periodic reports that need to be run. I've set up a pivot table that can be updated monthly with departments in column labels and order data on row labels. What I'm looking to achieve is generating output specific to users. That means: a single excel file tailored to a department.

What I've gotten so far:

Code:
Sub PivotLoop()


Dim PvTable As PivotTable
Dim PvField As PivotField
Dim PvItem As PivotItem
Dim PvItemL As PivotItem
Set PvTable = ActiveSheet.PivotTables("PivotTable2")
Set PvField = PvTable.PivotFields("Department")




For Each PvItem In PvField.PivotItems
    For Each PvItemL In PvField.PivotItems
        If PvItemL = PvItem Then
            PvField.PivotItems(PvItemL).Visible = True 'Error occurs here
            Else: PvField.PivotItems(PvItemL).Visible = False
        End If

    Next
    ' code to save file goes here
Next
End Sub

However, this loop yields an error: Run-time error '1004': unable to get the PivotItems property of the PivotField class. Can you guys help me adjusting this code?
Thanks for all input!
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Instead of:

Code:
 PvField.PivotItems(PvItemL).Visible = True

try

Code:
 PvItemL.Visible = True
 
Upvote 0
Instead of:

Code:
 PvField.PivotItems(PvItemL).Visible = True

try

Code:
 PvItemL.Visible = True

This works, thank you!
The next problem that I encounter is that for the 1st loop on the 2nd iteration all items are hidden so again I see an error. Is there a quick way to show all items?
 
Upvote 0
Sorry, ignore my previous post. I think the problem is that in the 2nd iteration a phase occurs where no value is selected, which cannot hold for a pivot table. Adding "on error resume next" results in the procedure skipping the 'last' value that would have been set as invisible and the logical result is that 2 values are shown in the table, which i don't want. Any and all suggestions?
 
Upvote 0
If you know what is causing the error, couldn't you add a test for that specific condition and do something else in your loop? :)
 
Upvote 0
Before:
For Each PvItemL

add the line:
PvField.PivotItems(PvField.PivotItems.Count-1).Visible=True

This will turn on the last item so that you don't have a case where all items have Visible set False.
 
Upvote 0

Forum statistics

Threads
1,203,547
Messages
6,056,038
Members
444,840
Latest member
RazzelDazel

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