user9940344
user9940344

Reputation: 584

How to access specific column totals for pivot table in VBA?

This is a very trivial question but I am struggling to find a answer to this online.

How do I "access" a pivot table grand total for a given column.

For instance I have the pivot table "pivottable1".

Customer   Product Code   Sum of price
   .   
   .             .
   .                           . 
 GRAND TOTAL                   $5000

So in this example I want to copy the $5000 into another cell.

The pivot table is dynamic and will change length.

I want to say for a given column Sum of price, take the grand total and copy it to another cell.

Upvotes: 0

Views: 1969

Answers (2)

Mikku
Mikku

Reputation: 6654

Notes:

Try:

Sub tst()

With ActiveSheet.PivotTables("PivotTable1")

ActiveSheet.Range("A1").Value = .PivotFields("Sum of Sum").DataRange.End(xlDown).Value

End With

End Sub

Demo:

enter image description here

Upvotes: 3

Justyna MK
Justyna MK

Reputation: 3563

Give it a try:

Sub foo()
    Dim lngRows As Long, lngColumns As Long, lngGT As Long

    With Sheet1.PivotTables(1).DataBodyRange
        lngRows = .Rows.Count
        lngColumns = .Columns.Count
        lngGT = .Cells(1).Offset(lngRows - 1, lngColumns - 1)
    End With

    Sheet1.Range("E11").Value = lngGT
End Sub

Result:

enter image description here

Upvotes: 3

Related Questions