Copy "Grand Total" from Pivot table, using VBA

axf80

New Member
Joined
Oct 8, 2015
Messages
13
I'm trying to use VBA to copy the Total column from a dynamic pivot table but keep getting an error.

Here is my code:

MyPivotTable.PivotFields("Name").PivotItems("Grand Total").DataRange.Copy
Sheets("Sheet 1").Range("A1").PasteSpecial xlPasteValues

This approach works fine for normal columns in the pivot table, but doesn't seem to work for the Sum column labelled "Grand Total". Do I need to refer to this column differently?

Many thanks
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I don't think there is a Grand Total pivot Item.

You can try this:

Code:
Option Explicit
Sub RowGrandTotal()
Dim pvt As PivotTable
Dim rng As Range


Set pvt = ActiveSheet.PivotTables("PivotTable3")
Set rng = Range(pvt.DataBodyRange.Cells(1).Offset(pvt.DataBodyRange.Rows.Count - 1, 0), pvt.DataBodyRange.Cells(1).Offset(pvt.DataBodyRange.Rows.Count - 1, pvt.DataBodyRange.Columns.Count - 1))
rng.Select


End Sub
Sub ColumnGrandTotal()
Dim pvt As PivotTable
Dim rng As Range


Set pvt = ActiveSheet.PivotTables("PivotTable3")
Set rng = Range(pvt.DataBodyRange.Cells(1).Offset(0, pvt.DataBodyRange.Columns.Count - 1), pvt.DataBodyRange.Cells(1).Offset(pvt.DataBodyRange.Rows.Count - 1, pvt.DataBodyRange.Columns.Count - 1))
rng.Select


End Sub
 
Upvote 0
Thanks for your response Comfy - I'm not quite sure what it's doing, but it works great! Just what I was after!
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,692
Members
449,117
Latest member
Aaagu

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