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>[TABLE="class: t1"]
<tbody>[TR]
[TD="class: td1"]Sum of All Costs[/TD]
[TD="class: td1"]Column Labels[/TD]
[TD="class: td1"][/TD]
[TD="class: td1"][/TD]
[TD="class: td1"][/TD]
[TD="class: td1"][/TD]
[TD="class: td1"][/TD]
[TD="class: td1"][/TD]
[TD="class: td1"][/TD]
[/TR]
[TR]
[TD="class: td2"]Row Labels[/TD]
[TD="class: td2"]A[/TD]
[TD="class: td2"]B[/TD]
[TD="class: td2"]C[/TD]
[TD="class: td2"]D[/TD]
[TD="class: td2"]E[/TD]
[TD="class: td2"]F[/TD]
[TD="class: td2"]G[/TD]
[TD="class: td2"]Grand Total[/TD]
[/TR]
[TR]
[TD="class: td3"]March[/TD]
[TD="class: td3"][/TD]
[TD="class: td3"][/TD]
[TD="class: td3"]$200[/TD]
[TD="class: td3"]$100[/TD]
[TD="class: td3"][/TD]
[TD="class: td3"][/TD]
[TD="class: td3"]$100[/TD]
[TD="class: td3"]$400[/TD]
[/TR]
[TR]
[TD="class: td3"]April[/TD]
[TD="class: td3"][/TD]
[TD="class: td3"][/TD]
[TD="class: td3"][/TD]
[TD="class: td3"][/TD]
[TD="class: td3"]$200[/TD]
[TD="class: td3"][/TD]
[TD="class: td3"][/TD]
[TD="class: td3"]$200[/TD]
[/TR]
[TR]
[TD="class: td3"]May[/TD]
[TD="class: td3"]$100[/TD]
[TD="class: td3"][/TD]
[TD="class: td3"][/TD]
[TD="class: td3"][/TD]
[TD="class: td3"][/TD]
[TD="class: td3"][/TD]
[TD="class: td3"][/TD]
[TD="class: td3"]$100[/TD]
[/TR]
[TR]
[TD="class: td3"]June[/TD]
[TD="class: td3"][/TD]
[TD="class: td3"]$100[/TD]
[TD="class: td3"][/TD]
[TD="class: td3"][/TD]
[TD="class: td3"][/TD]
[TD="class: td3"]$5,789[/TD]
[TD="class: td3"][/TD]
[TD="class: td3"]$5,889[/TD]
[/TR]
[TR]
[TD="class: td4"]Grand Total[/TD]
[TD="class: td4"]$100[/TD]
[TD="class: td4"]$100[/TD]
[TD="class: td4"]$200[/TD]
[TD="class: td4"]$100[/TD]
[TD="class: td4"]$200[/TD]
[TD="class: td4"]$5,789[/TD]
[TD="class: td4"]$100[/TD]
[TD="class: td4"]$714,005[/TD]
[/TR]
</tbody>[/TABLE]

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

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Forum statistics

Threads
1,224,823
Messages
6,181,185
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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