How to Update a PivotTable Date Filter with a VBA Button in Excel
Question details
The user needs to update the 'Available Month' date filter for multiple PivotTables on a single sheet using a date value from cell B1, triggered manually by clicking a VBA button instead of an automatic worksheet change event.

- Product
- Excel / WPS Spreadsheet
- Device & OS
- not provided
- Scenario
- Automating the synchronization of date filters across multiple PivotTables using a specific cell's value, but only executing the change when a user clicks a designated form button.
- Observed behavior
- The user needs to prevent the macro from running automatically via Worksheet_Change and ensure that the date value applied to the PivotTable's CurrentPage property is correctly formatted to avoid VBA runtime errors.
Ensure that the Developer tab is enabled in your ribbon to access the VBA Editor and insert form controls. Also, verify that your workbook is saved as a Macro-Enabled Workbook (.xlsm) so you do not lose your code.
Create a Standard Subroutine and Assign it to a Button
Move the update logic from the Worksheet_Change event to a standard VBA module and assign it to a form control button for manual triggering, ensuring correct date formatting.
Using a Worksheet_Change event can cause the macro to trigger prematurely while you are still typing or editing the cell. Moving the code to a standard module and linking it to a button gives you complete control over when the PivotTables refresh.
When filtering PivotTables by date using VBA, sending a raw date value often causes errors. You must format the value to match exactly how the date appears in the PivotTable dropdown (e.g., 'd-mmm' or 'm/d/yyyy').
Press Alt + F11 to open the Visual Basic for Applications (VBA) Editor. Right-click on your workbook in the Project Explorer, select 'Insert', and choose 'Module' to create a standard module.
In the new module, create a Sub (e.g., `Sub UpdatePivotDates()`). Write a `For Each` loop to iterate through all PivotTables on your target sheet: `For Each pt In Worksheets("Pivot").PivotTables`.
Inside the loop, clear the existing filter and set the CurrentPage property using the `Format$` function to prevent date mismatch errors. Example: `pt.PivotFields("Available Month").CurrentPage = Format$(Worksheets("Macro").Range("B1").Value, "d-mmm")`.
Close the VBA Editor and return to your worksheet. Go to the Developer tab, click 'Insert', and select 'Button (Form Control)'. Draw the button on your sheet.
An 'Assign Macro' dialog box will appear immediately after drawing the button. Select the `UpdatePivotDates` macro you just created and click 'OK'.

Manage PivotTables and VBA Macros in WPS Spreadsheet
WPS Spreadsheet offers powerful PivotTable features and macro compatibility, allowing you to easily manage and automate complex data analysis tasks just like in Microsoft Excel.
- 1. Open Your Macro-Enabled Workbook: Launch WPS Spreadsheet and open your existing .xlsm file containing the PivotTables.
- 2. Access the Developer Tools: Navigate to the Developer tab on the ribbon. Click on 'Macros' or 'VBA Editor' to view and modify your PivotTable scripts.
- 3. Insert Form Controls: Use the 'Insert' tool within the Developer tab to place a button on your sheet and easily assign your VBA scripts for instant PivotTable filtering.

Frequently Asked Questions
Why do I get a runtime error when applying a cell value to the PivotTable date filter via VBA?
This usually happens because of date format mismatches between the raw cell value and the PivotTable field. Ensure you format the cell value in VBA using the `Format$` function—like `Format$(Range("B1").Value, "d-mmm")` or `"m/d/yyyy"`—to match exactly how the dates are formatted inside the PivotTable's filter dropdown.
How do I stop my macro from running automatically every time I type in the reference cell?
You need to remove your VBA code from the `Worksheet_Change` event located in the specific Sheet module. Instead, place the code inside a standard module (e.g., Module1) within a standard `Sub`, and then assign that `Sub` to a clickable form control button on your worksheet.
Can I update multiple PivotTables at once with a single button click?
Yes. By using a loop such as `For Each pt In Worksheets("Pivot").PivotTables` in your VBA code, you can iterate through all the PivotTables on that specific worksheet and apply the same filter update to all of them instantly when the button is clicked.
Does WPS Office support VBA macros for controlling PivotTables?
Yes, WPS Office supports VBA macros. Ensure you have the VBA module installed (included in advanced or pro versions, and available as an add-on for others) to run macros that manipulate PivotTables and automate tasks.




