VBA To Control PivotTable Filtering

MikeG

Well-known Member
Joined
Jul 4, 2004
Messages
845
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have the following code that helps a user to control a PivotTable without having to use the dropdown filter. In a cell called "Selected_Store1", the user enters a store number - e.g. 347. They then hit a button that triggers the code below.

k = Range("Selected_Store1")

With ActiveSheet.PivotTables("PivotTable12").PivotFields("Store #")
.PivotItems(k).Visible = True
End With


I have two things I would like help on.

First, how can I clear any existing stores that are checked in the drop down filter box? (If I am doing it manually, I either uncheck the store(s) in question, or check Select All, then click Select All to clear.)

Secondly, the code

.PivotItems(k).Visible = True

worked OK when I hard code in a value such as:
.PivotItems("347").Visible = True

but does not work when I use the variable k. How can I make it so that k is read properly as a string (I guess)?

Thanks,

MikeG
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Forum statistics

Threads
1,224,551
Messages
6,179,472
Members
452,915
Latest member
hannnahheileen

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