Loop through each row in a Pivot Table

PMCODER

New Member
Joined
May 9, 2017
Messages
1
Hello all,

I tried searching the forums and cannot figure this out. I am new to VBA but have experience with C and PHP.

I have a Pivot Table grouped by State & Name in one worksheet and all other data spread across other worksheets.

A sample of the Pivot table data is below:
Row LabelsSum of ShoesSum of Shoes - VoidSum of BootsSum of Boots - VoidSum of SocksSum of Socks - VoidSum of Total
AL341544348712814366
John A. Smith7100008
John Smith4935444130595
James Bond102000012
AR7710820097
User: Smith12, John7210820092
Baba Dook5000005
AZ332927901664514324
Dane Great6000006
Greet V. Bob33224001225483
User: Smith16, John4318007068

<tbody>
</tbody>

---------------------

I am attempting to loop through each state in the pivot table to:
1. Retrieve state level data from another worksheet and copy to the top row for the state. Will do this once for each state.

2. Loop through all employees for that specific state and retrieve employee level data. The challenges here are the employee names are stored in full name format in HR and other systems (<last name="">, <first name=""> <middle name=""> etc) so I have to check the employee last name for the state and copy only the matching data to the destination worksheet.

Below is a desired output based on the above:
Row LabelsSum of ShoesSum of Shoes - VoidSum of BootsSum of Boots - VoidSum of SocksSum of Socks - VoidSum of TotalShoes
External Count
Boots
External Count
Socks
External Count
Sum of External CountEmp Response CountEmp Void CountEmp Commission RateOther Emp Data4
AL341544348712814366386750094376
John A. Smith7100008XXXXXXXXX * YYYXXX
John Smith4935444130595XXXXXXXXX * YYYXXX
James Bond102000012XXXXXXXXX * YYYXXX
AR77108200978710097
User: Smith12, John7210820092XXXXXXXXX * YYYXXX
Baba Dook5000005XXXXXXXXX * YYYXXX
AZ332927901664514324361117184330
Dane Great6000006XXXXXXXXX * YYYXXX
Greet V. Bob33224001225483XXXXXXXXX * YYYXXX
User: Smith16, John4318007068XXXXXXXXX * YYYXXX

<tbody>
</tbody>

<tbody>
</tbody>

Can you please give ideas on how to code this?

Here is the code that I used to loop through the elements and is not working as expected:

Set stateCode = pvt.pivotFields("STATE_CODE")
Set acoName = pvt.pivotFields("ACO_NAME")

For Each statePivotItem In stateCode.PivotItems
stateCode.ClearAllFilters
stateCode.CurrentPage = statePivotItem.Name

MsgBox "State is " & statePivotItem.Value 'Works well

'Copy all state level data to the appropriate cells

For Each itm In fld.PivotItems
MsgBox "Item is " & itm.Value 'Displays ALL employees not just this state's employees

'Do other stuff to copy other data based on ACO Name & State code​
Next​
Next


Many thanks!</middle></first></last>
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Forum statistics

Threads
1,214,397
Messages
6,119,273
Members
448,883
Latest member
fyfe54

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