Macro to drilldown on a specific pivot table row item

ddander54

Board Regular
Joined
Oct 18, 2012
Messages
97
Asking for some help with this code found thru google that I'm attempting to use on any pivot table. The goal is to be able to select any row item in a pivot table and then have a macro 'drilldown' to the last column for that row, similar to clicking all the '+' signs to expand that row item.
I'm currently getting a Run-Time error 438 at the 'Set pi = pt.ActiveCell.PivotCell.PivotItem' line. I believe the error is telling me that ActiveCell is not a valid reference in a pivot table, but I'm not sure how to correct the problem.
I can see that 'pt' is the pivot table name and 'ActiveCell' is the pivot table item name, but when I put the two together I'm getting the same error 438 at the 'msgBox pt.ActiveCell' line as I am at the 'Set pi = pt.ActiveCell.PivotCell.PivotItem' line.

TIA,
Don

VBA Code:
Option Explicit


Sub DrillDownToSpecificField()
    Dim pt As PivotTable
    Dim pi As PivotItem
    Dim pl As PivotLine
    
    Set pt = ActiveCell.PivotTable
    MsgBox pt
    MsgBox ActiveCell
'    MsgBox pt.ActiveCell
    Set pi = pt.ActiveCell.PivotCell.PivotItem  '<<< Run-time error 438 here
    Set pl = pt.ActiveCell.PivotCell.PivotLine
    
    pt.DrillDown pi, pl
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
In case others are looking for a similar solution, this is what I came up with.....assigned to buttons.

VBA Code:
Sub Expand()
    'declare a range object
    Dim myVariable As Range
    'set the value of range variable to active cell
    Set myVariable = ActiveCell
    'do something with the variable
    'Range("A13").Select
    'MsgBox myVariable
    ActiveSheet.PivotTables("PivotTable2").PivotFields("Resource_Pool").PivotItems( _
        myVariable.Value).DrillTo "resources_requested_name"
End Sub

Code:
Sub Collapse()
    'declare a range object
    Dim myVariable As Range
    'set the value of range variable to active cell
    Set myVariable = ActiveCell
    'do something with the variable
    'Range("A13").Select
    'MsgBox myVariable
    ActiveSheet.PivotTables("PivotTable2").PivotFields("Resource_Pool").PivotItems( _
        myVariable.Value).DrillTo "Resource_Pool"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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