Return Pivot Data Grand Total In VBA

Domski

Well-known Member
Joined
Jan 18, 2005
Messages
7,292
Hi,

I have a sheet with about twenty pivot tables whose page fields are controlled with VBA.

What I want to do is test each table to find out if the grand total is zero.

I thought this might return the total:


Code:
ActiveSheet.PivotTables("PivotTable1").PivotFields("M/Km").Value


in a similar way to the GETPIVOTDATA function works but all it returns is M/Km.

I guess I could use the GETPIVOTDATA function in VBA but thought there might be a better way.

Cheers,

Dom
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Does this give you what you want?

Code:
Sub Test()
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("M/Km").DataRange
        MsgBox .Cells(1, 1).Offset(.Rows.Count, 0).Value
    End With
End Sub
[code]
 
Upvote 0
Hi Andrew,

Tried that and I get a Run-Time error '1004':

Unable to get the DatyaRange property of the PivotField class.

Dom
 
Upvote 0
Thanks, it worked if I changed the field name to that.

Much appreciated.

Dom
 
Upvote 0
Does anyone know how to return data within a Pivot Table, without referencing a specific range or cell in the VBA code?

The reason I ask is, the pivot table may change (i.e., additional rows, additional columns) and I'd like to be able to return the subtotals/grand total without having to update the VBA code.
 
Upvote 0

Forum statistics

Threads
1,215,417
Messages
6,124,791
Members
449,188
Latest member
Hoffk036

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