VBA, filtering pivot table based on cell value

sanantonio

Board Regular
Joined
Oct 26, 2021
Messages
124
Office Version
  1. 365
Platform
  1. Windows
VBA Code:
Sub FilterPivot_SingleCell()
Dim PV_WS As Worksheet
Dim Pv_Table As PivotTable
Dim Pv_Field As PivotField
Dim Fltr_KW As String
'Assigning Worksheet of Pivot Table
Set PV_WS = Worksheets("Report")
'Assigning PivotTable Name
Set Pv_Table = PV_WS.PivotTables("PivotTable12")
'Assigning PivotTable Field to Filter
Set Pv_Field = Pv_Table.PivotFields("Short")
'Assigning Cell Value to the Filter KeyWord
Fltr_KW = Range("B3").Value
'Clearing all filters
Pv_Field.ClearAllFilters
'Setting Filters to the cell value
Pv_Field.CurrentPage = Fltr_KW
End Sub

My pivot table name is correct, and it's on the worksheet called "Report", the filter is called "Short" and there is a value in B3.

I've had the code in "This Workbook", in the "Report" worksheet and in a module and it just doesn't seem to work.

I've had a similar macro to this working some years ago but I can't find my version so this has come from a quick google but it doesn't seem to work.

Any ideas?

1700145536498.png
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Works for me.

You could upload a copy of your file or just "Report" sheet to a free site such www.dropbox.com or google drive.
Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here.
If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0
Works for me.

You could upload a copy of your file or just "Report" sheet to a free site such www.dropbox.com or google drive.
Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here.
If the workbook contains confidential information, you could replace it with generic data.

That would be great please, I'm sure I'm just missing something simple. Particularly frustrating when I've had this macro working in the past!


Is a link to a workbook with rubbish data with the macro in and I still can't get it to function. Any help would be appreciated.

I don't have much confidence in the above link working as it doesn't work for me but it's all google drive will give me :-/
 
Upvote 0
I am having the same issue as Dante, that code works for me. If you are going to give sample data you really need to give representative data.
Your original data has merged cells which are not in the sample you provided also you have different sheet names, addresses & pivot table name.

Did you try running the code on the data you sent us ?
Does it really not work even on that sample data ?

When you say doesn't work, what does that mean ?
Are you getting an error message, if so what is it and when you press debug which line is highlighted ?
As it stands if the filter value doesn't exist you will get an 1004 error

Try adding the debug.print line as shown below and see if it picking up your filter value.
(view in the immediate window, Ctrl+G if you can't see the window)

Rich (BB code):
Debug.Print Fltr_KW
'Setting Filters to the cell value
Pv_Field.CurrentPage = Fltr_KW
 
Upvote 0

Forum statistics

Threads
1,215,077
Messages
6,122,992
Members
449,094
Latest member
masterms

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