@
DSP Varma
Interesting and
almost crazy problem to have. I think if you have actual pivots in a worksheet (your example file is just paste values of pivots), then you can use VBA to loop thru all of them like below.
Code:
Sub showGTs()
Dim pt As PivotTable, ws As Worksheet
Set ws = ActiveSheet
On Error Resume Next
For Each pt In ws.PivotTables
Debug.Print pt.GetData(pt.GrandTotalName)
Next pt
End Sub
As we don't have access to your workbook, I am not sure if the commission rate is a pivot report filter or value typed in. If it is part of report filters, you can pull that too, using below code.
Debug.Print pt.PivotFields("Commission Rate ").CurrentPage
Once you have both values, you can then calculate the total commission easily.
That said,
if you have access to Power Query, then you can use it. In fact, I recommend using PQ to strip this data to extract Commission Rates and Grand Totals because it is just too much fun.
Using Power Query to extract commission rates, grand totals as a table:
- Save your attachment as a XLS or CSV file.
- Create a new file (or open one of your existing ones)
- Go to PQ > From File > From Excel (or CSV)
- Select entire spreadsheet
- Click on "Edit" to Bring the raw pivot table data in to PQ
- Filter Column 1 to show any rows with Commission Rate or Grand Total
- This removes all clutter and leaves 2 rows per pivot (one with Commission Rate and another with Grand total row)
- Name this query as "Data"
- Expand the queries pane on left and right click on "Data" query and duplicate it.
- Name the newly created query as "Commission Rates"
- In commission rates query
- Filter column 1 so only commission rates remain
- Select first 2 columns, right click on column headers and choose "Remove other columns"
- Add column with index numbers starting from 1
- Rename this column as "Pivot number"
- Rename first 2 columns as Label and Rate
- We are done with this now. Go back to "Data" query.
- In Data Query
- Filter away "Commission Rate" from column 1 so only Grand Totals remain.
- Add column with index numbers starting from 1.
- Rename this column as "Pivot num"
- Move the "Pivot num" column all the way to front
- Select Pivot num and Column 1 and right click on headers
- Choose "Unpivot other columns"
- Select first two columns now
- Go to transform > Group By
- Group by "Pivot num" and "Column 1" with aggregation filed as "Total" with Max of Value column


- Click ok and your grand totals from all pivots (where GT is not null) is ready.
- Time for the merge
- From Home ribbon, click on Merge queries
- Merge Data query with Commission Rates query on Pivot num column
- In the new column, just expand Rate alone
- Select the newly created rate column
- Go to Add Column > Standard > Multiply
- Choose Total as multiplication column
- Rename "inserted multiplication" column as "Commission"
- Rename other columns as you see fit
- Close and load only the data query to your workbook.
Done!
When you add more pivots to your frankenworkbook, just refresh the query in your new file. And all rates fetched.
Note: This assumes all pivots will have Grand Total field named as "Grand Total". If you change this, you need to adjust the steps in PQ. Also, each pivot will have a commission rate row just above it. It can be a report filter or manually typed in thing. Doesn't matter.
See attached workbook. Change query connection to the raw data file you uploaded in post #1.