VBA: Looping through PivotTable rows and columns (including row fields)

colinhahn

New Member
Joined
Jan 4, 2021
Messages
11
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I am trying to write some VBA to loop through a PivotTable, and I am struggling with the appropriate objects and names.

My PivotTable has a handful of filters (the code needs to be able to accomodate a changing number here), 7 row fields (this will be unchanging), and one column field (the one field will be unchanging, but the number of items contained in that column in the dataset will change and so the code needs to be able to dynamically adjust for the table taking up more or less total columns).

What I want to do is loop through the table: starting with the first row of row field data, go through the other row fields and populate some variables with row-specific information. Then, loop through the data columns for that row and call a subroutine with the value in each cell. Repeat the process through each row of the table.

In the sample table below: the first row would be the row starting with Value A/Value B. The code would populate the variables as appropriate for the row fields, then would start looping through the column fields. In this caes, there are two columns and so it would call a subroutine twice, first with "101" as the parameter and second with "201". Then, the next row would be Value A/Value C, with the inner loop calling the subroutine with the column values of "245" and "122."

Filter 1 nameFilter 1 dropdown
Filter 2 nameFilter 2 dropdown
Value field nameColumn field title
Row field 1 nameRow field 2 name(...rest of field rows)Column field 1 Column field 2
Value AValue B...101201
Value AValue C...245122

Bonus: I'd also like to be able to start and end from a specified row of the sheet. So, if the first row of table content (not headers) is row 6 on the sheet, I could tell the VBA to start at (sheet) row 8 and it would be able to figure out that I need the third row of table data.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Forum statistics

Threads
1,214,588
Messages
6,120,412
Members
448,959
Latest member
camelliaCase

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