This is a code which maybe you can figure out to implement it to your code :
Sub test()
Set sht = Sheets("Sheet1")
Set pt1 = sht.PivotTables(1)
Set pt2 = sht.PivotTables(2)
Range("E26").Value = pt1.GetPivotData("SUM").Value
Range("B26").Value = pt2.GetPivotData("SUM").Value
End Sub
the grand total needs to be copied over to the next empty row in
column R
Oops... sorry,
so your problem is the placement of the copied GrandTotal :).
I've found a code that can copy the grand total to a cell but I
can't make it work to copy it in the next empty cell
Sheets("Sheet1").Cells(LastRow, 18).Select
If you step-run the code above, is the result of the active cell correct ? Which as you expected that the result of the active cell is the next empty cell.
If it's not correct, then maybe you need to add .offset(1,0)
before the .select
. But still I don't know what is the value of your 'LastRow' variable. If the value is the last row number in the sheet (where mine is 1048576), then maybe you need to put .end(xlup).offset(1,0)
If it's correct, I wonder what is the problem ?
It also can be done with the code above :
Sheets("Sheet1").Cells(LastRow, 18).value = pt1.GetPivotData("SUM").Value
The word "SUM" is depend on what is your pivot table GrandTotal column header name. Mine is SUM as seen in the image above.
This code is not depend on the row where the pivot table GrandTotal displayed. So you don't have to code to find the word for the pivot table GrandTotal first then offset(0,1) to get the value. If for example at first the GrandTotal display in row 200, and after data source change and refresh the GrandTotal display in row 1000, it will always give you the correct GrandTotal.