How to filter a Pivot Table using VBA when filtering for Values (Measures) has been turned on

Amir 36 Reputation points
2021-10-12T12:47:23.1+00:00

Hello,

I have a pivot table built from a Data Model containing measures I have created using DAX.

Using the technique demonstrated in this video https://www.youtube.com/watch?v=40wE-oKG2zw, I have switched on filtering for my measures. Everything is working well: I have a (DAX) measure called NonZeroBalance which returns Yes or No. I am then using this helper column to filter out rows with zero balance.

My problem is that the filtering works well if I use Excel interactively. But, when the workbook is refreshed using VBA (saved with filtered on NoZeroBalance = "Yes"), the filter stops working, and Excel shows rows with zero balance. The code below doesn't let me re-apply the filter.

Any ideas?

Dim pt As PivotTable
Dim pf As PivotField

Set pt = ThisWorkbook.Worksheets("Segments").PivotTables("ptSegments")
Set pf = pt.PivotFields(10)
pf.ClearAllFilters
pf.CurrentPage = "Yes"   **<-- Unable to set the CurrentPage property of the PivotField class**

I have tried other VBA code, including the macro that I recorded using Excel:

Sub ReApplyFilter()

'
' ReApplyFilter Macro
'
ActiveSheet.Range("$B$4:$K$173").AutoFilter Field:=10, Criteria1:="Yes"

End Sub

0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.