Filter pivot table (from power pivot) based on cell value

devtr

New Member
Joined
Jan 23, 2021
Messages
6
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hello Excel gurus,
There are several examples of this task as long as it is a simple pivot table based on one table.
I am using multiple tables with power pivot. The filter field in this pivot table comes from a different table.
I have used the code below. It shows Type mismatch error# 13, no matter what changes I make to the data.

VBA Code:
Private Sub Workbook_sheetChange(ByVal Sh As Object, ByVal Target As Range)

'Set the Variables to be used
Dim pt As PivotTable
Dim Field As PivotField
Dim NewCat As Long

NewCat = Worksheets(7).Range("B9").Value

Set pt = Worksheets(8).PivotTables("PTYOYSLLastYear")
Set Field = pt.PivotFields("[TblDateMast].[Date_Mast (Year)].[Date_Mast (Year)]").VisibleItemsList = _
    ("[TblDateMast].[Date_Mast (Year)]." & NewCat)

'This updates and refreshes the PIVOT table
With pt
Field.ClearAllFilters
Field.CurrentPage = NewCat
pt.RefreshTable
End With


End Sub

The error shows the "Set Field =" line.
TblDateMast simply has the dates with day numbers, month, quarters, and years columns.
The other revenue-related information comes from the TblRev table.

The pivot table and the cell value to filter the pivot table are in different worksheets.
I am trying to filter years.

Thank you in advance.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

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