Filter Pivot Table based on Cell value, if no value then select ALL

John_356

New Member
Joined
Jan 17, 2021
Messages
26
Office Version
  1. 365
Platform
  1. Windows
Good day,

Browsing through the forum I have found this code which works for me. I would need an additional function, if J14 = "" then filter to ALL instead of Exit Sub:

VBA Code:
[B]Private[/B] [B]Sub[/B] Worksheet_SelectionChange([B]ByVal[/B] Target [B]As[/B] Range)

If Intersect(Target, Range("J14")) Is Nothing Then Exit Sub

Dim pt As PivotTable
Dim Field As PivotField
Dim NewCat As String
Set pt = Worksheets("sheet1").PivotTables("pivottable1")
Set Field = pt.PivotFields("Month")
NewCat = Worksheets("sheet2").Range("J14").Value

With pt
Field.ClearAllFilters
Field.CurrentPage = NewCat
pt.RefreshTable
End With

[B]End Sub[/B]

Many thanks in advance.

John
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
UPDATE

I've tried using the code with below changes, yet again without success:

VBA Code:
If Intersect(Target, Range("J17")) Is Nothing Then ActiveSheet.PivotTables("pivottable1").PivotFields("Year").CurrentPage = "(All)"
    ActiveSheet.PivotTables("pivottable1").PivotFields("Month").EnableMultiplePageItems = _
    True
Set pt = Worksheets("sheet1").PivotTables("pivottable1")
Set Field = pt.PivotFields("Year")
NewCat = Worksheets("MACRO").Range("J17").Value
With pt
Field.ClearAllFilters
Field.CurrentPage = NewCat
pt.RefreshTable
End With

FYI I'll be running the code twice as I have two filters, Month and Year, if they're not selected and their cells are blank I would like the code to filter to ALL Years and Months available. If only one is selected then filtering applies to that one, with the other one set to ALL once more.

Thanks again!

J
 
Upvote 0

Forum statistics

Threads
1,215,005
Messages
6,122,661
Members
449,091
Latest member
peppernaut

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