VBA to change Power Pivot Filters

Andrew_UK

Board Regular
Joined
Jun 3, 2015
Messages
53
Hi all,

I have built a pivot table using power pivot; it's set to tabular view so that Customer Code and Company Name are in columns A and B. When a user clicks on the Company Code or Company Name then the Macro needs to jump to the P&L Sheet and change the customer Code to match...

Code:
Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    Dim pt As PivotTable
    Dim Custfield As PivotField
    Dim Salesfield As PivotField
    
    Dim i As Integer
    Dim a As String
    
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("A:B")) Is Nothing Then
            i = ActiveCell.Row
            a = Cells(i, 1).Value
            
Sheets("P&L Sheet").Select
Sheets("P&L Sheet").Activate
            
Set pt = Sheets("P&L Sheet").PivotTables("PivotTable1")
Set Custfield = pt.PivotFields("[FullCustList].[CODE].[CODE]")
            
With pt.PivotFields(Custfield.Name)
    .ClearAllFilters
    .CurrentPage = a
End With
        
        End If
    End If
    Application.ScreenUpdating = True
    Exit Sub


End Sub

The point at which it's erroring out is "CurrentPage=a", the error message I get is

"Run-time error '1004':

Unable to set the CurrentPage property of the PivotField Class"

I've tried recording the Macro, but when I then hit play again it doesn't work either. Any ideas?

Thanks in advance!!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Forum statistics

Threads
1,215,055
Messages
6,122,902
Members
449,097
Latest member
dbomb1414

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