For Each Loop in Pivot Table

jmon

New Member
Joined
May 18, 2023
Messages
8
Office Version
  1. 365
Platform
  1. Windows
I am trying to write a for each loop that will run through a pivot table and drill down to one of the values in the pivot table (creating a new tab for each value). Using the info from the sample picture, I am trying to get a drill down from the total for each company (i.e., Company A's total is 78, B's is 119, etc.). I've searched all over and cannot seem to find any code that works in this case (or perhaps that I can understand).

I am able to make this work if I went through and called out each "Bill To" separately, but that won't work as the list changes. I desperately need help figuring out how I can loop through the list and extract a new tab for each "Bill to" by referencing the pivot table itself. Any help would be greatly appreciated.

Here is a snip of the existing code:

Windows("Sample").Activate
ThisWorkbook.Activate
Sheets("Sheet2").Select

Dim PvtTbl As PivotTable
Dim PvtFlds As PivotField
Dim PvtFld As PivotField
Dim Total As PivotField
Dim BillTo As PivotField


Set PvtTbl = Sheets("Sheet2").PivotTables("Pivottable2")
Set BillTo = PvtTbl.PivotFields("Bill To")
Set Total = PvtTbl.PivotFields("Total")

With Sheets("Sheet2").PivotTables("Pivottable2")
Dim d As Variant
On Error GoTo Error
With Sheets("Invoicing").PivotTables("PivotTable3")
d = .GetPivotData("Total", "BillTo")
d = .GetPivotData("Total", "BillTo").Address
Range(d).ShowDetail = True
End With
****perform actions with sheets

Next
End With


Sample.xlsx
DEFGHI
1Row LabelsSum of WigwamCount of WigitSum of ThisSum of ThatSum of Total
2Company A335172178
3Bronze17281142
4Copper815014
5Gold8101018
6Silver01404
7Company B2394420119
8Copper154221052
9Gold24171050
10Silver615017
11Company C80164548235
12Bronze1923436
13Copper9211536
14Gold1831533
15Silver3493034130
16(blank)
17(blank)
18Grand Total1363010689432
Sheet2
 

Attachments

  • 1684467953298.png
    1684467953298.png
    28.1 KB · Views: 7
:unsure: Very weird :unsure:

Try the following change:
VBA Code:
Sub ForEachPivotTable()
  Dim sh As Worksheet
  Dim PvtTbl As PivotTable
  Dim BillTo As PivotField
  Dim pItem As PivotItem
  Dim d As String
 
  Set sh = Sheets("Sheet2")
  Set PvtTbl = sh.PivotTables("Pivottable2")
  Set BillTo = PvtTbl.PivotFields("Bill To")
 
  For Each pItem In BillTo.PivotItems
    sh.Select
    d = pItem.DataRange.Offset(-1, 4).Address
    Range(d).ShowDetail = True
  Next
End Sub
No luck. Still getting the same run time error on Range(d).ShowDetail = True
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
I think I found the issue. If I change this line:

d = pItem.DataRange.Offset(-1, 4).Address

to:

d = pItem.DataRange.Offset(0, 4).Address

It works properly.

Would you happen to know a quick way to have it ignore the (blank) line in the pivot table?
 
Upvote 0
d = pItem.DataRange.Offset(0, 4).Address
Something like that I supposed.
But with 0 in the new sheet is the data you need to see correct?


Would you happen to know a quick way to have it ignore the (blank) line in the pivot table?
Try this:

VBA Code:
Sub ForEachPivotTable()
  Dim sh As Worksheet
  Dim PvtTbl As PivotTable
  Dim BillTo As PivotField
  Dim pItem As PivotItem
  Dim d As String
 
  Set sh = Sheets("Sheet2")
  Set PvtTbl = sh.PivotTables("Pivottable2")
  Set BillTo = PvtTbl.PivotFields("Bill To")
 
  For Each pItem In BillTo.PivotItems
    If pItem.Name <> "(blank)" Then
      d = pItem.DataRange.Offset(0, 4).Address
      sh.Range(d).ShowDetail = True
    End If
  Next
End Sub
 
Upvote 0
Solution
Something like that I supposed.
But with 0 in the new sheet is the data you need to see correct?
Yes, it is. I determined it was pulling the wrong column after using debug.print. The values it returned for (-1, 4) were incorrect.

Thank you for the additional code, that worked perfectly.

And thank you for all your help! It is greatly appreciated!!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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