[pivot table] Get rows total column with VBA

Lece

New Member
Joined
Jan 14, 2012
Messages
15
Hi!

I need to access all rows' total column in a Pivot Table with VBA. Something similar to
Code:
.PivotFields("MyColumns").PivotItems("Madrid").DataRange.Select
but only with the Total column

What I have is:


What I need is:


I know I can do something like this:
Code:
Range(4, 1).Select
Selection.End(xlToRight).Select
' Skip one cell down with .Offset here...
Range(Selection, Selection.End(xlDown)).Select
' Skip one cell up with .Offset here...
but I need a more flexible solution -- something like with Madrid above.

Tried to search for an answer here http://peltiertech.com/WordPress/referencing-pivot-table-ranges-in-vba/ but didn't find any.

Please help me! Thank you
 
Last edited:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi Lece,

Try...
Code:
Sub SelectTotalColumns()
 
    Dim lColFields As Long
 
    With ActiveSheet.PivotTables("PivotTable1")
        lColFields = .ColumnFields.Count
        If .RowGrand = False Or lColFields = 0 Then Exit Sub
        With .DataBodyRange
            .Resize(, lColFields).Offset(0, .Columns.Count - lColFields).Select
        End With
    End With
End Sub
 
Upvote 0
Hi JS411

Your code worked but I didn't need the total row selected so this is how I edited it:
Code:
    With ActiveSheet.PivotTables(1)
        lColFields = .ColumnFields.Count
        If .RowGrand = False Or lColFields = 0 Then Exit Sub
        With .DataBodyRange
            .Resize([B].Rows.Count - 1[/B], lColFields).Offset(0, .Columns.Count - lColFields).Select
        End With
    End With

Thank you very much for your help!
 
Upvote 0

Forum statistics

Threads
1,226,110
Messages
6,188,988
Members
453,519
Latest member
pat3pet

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