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]
Is there a way to always get the grandtotal associated with the last item in the row?
Thanks for the help!
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!