use GETPIVOTDATA to get specific grandtotals

taimy20

New Member
Joined
Jun 7, 2017
Messages
1
Hey guys! The forum has been great so far - thanks for everything you do.

So I've generated a PivotTable and need to retrieve the grand total associated with the last row item in the PivotTable using VBA. What I have currently gets me month-specific data, but once we add more months, the code will be useless:

<style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Helvetica}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; text-align: right; font: 11.0px Helvetica; min-height: 13.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; text-align: right; font: 11.0px Helvetica}table.t1 {border-collapse: collapse}td.td1 {background-color: #d4feff; border-style: solid; border-width: 1.0px 1.0px 1.0px 1.0px; border-color: #cbcbcb #cbcbcb #cbcbcb #cbcbcb; padding: 0.0px 5.0px 0.0px 5.0px}td.td2 {background-color: #d4feff; border-style: solid; border-width: 1.0px 1.0px 0.8px 1.0px; border-color: #cbcbcb #cbcbcb #a8d6ff #cbcbcb; padding: 0.0px 5.0px 0.0px 5.0px}td.td3 {border-style: solid; border-width: 1.0px 1.0px 1.0px 1.0px; border-color: #cbcbcb #cbcbcb #cbcbcb #cbcbcb; padding: 0.0px 5.0px 0.0px 5.0px}td.td4 {background-color: #d4feff; border-style: solid; border-width: 0.8px 1.0px 1.0px 1.0px; border-color: #a8d6ff #cbcbcb #cbcbcb #cbcbcb; padding: 0.0px 5.0px 0.0px 5.0px}</style>
Sum of All CostsColumn Labels
Row LabelsABCDEFGGrand Total
March$200$100$100$400
April$200$200
May$100$100
June$100$5,789$5,889
Grand Total$100$100$200$100$200$5,789$100$714,005

<tbody>
</tbody>

Code:
Worksheets("Spend Analysis 2").Activate
    Dim CurrentMonthSpend As Range
    Range("A3").Select
    Set CurrentMonthSpend = ActiveCell. _
        PivotTable.GetPivotData("All Costs", "Month", "June")
    Dim PreviousMonthSpend As Range
    Set PreviousMonthSpend = ActiveCell. _
        PivotTable.GetPivotData("All Costs", "Month", "May")

Is there a way to always get the grandtotal associated with the last item in the row?

Thanks for the help!
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Forum statistics

Threads
1,215,842
Messages
6,127,235
Members
449,372
Latest member
charlottedv

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