Pivot Table Issues

How to Update a PivotTable Date Filter with a VBA Button in Excel

Ayan MasoodAyan Masood Oct 28, 2026 869 views

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.

How to Update a PivotTable Date Filter with a VBA Button
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.
Before you start

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.

Solution 1Recommended

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').

1
Open the VBA Editor

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.

2
Write the PivotTable Loop Code

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`.

3
Apply Formatted Date to the Filter

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")`.

4
Insert a Form Control Button

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.

5
Assign the Macro to the Button

An 'Assign Macro' dialog box will appear immediately after drawing the button. Select the `UpdatePivotDates` macro you just created and click 'OK'.

Create a Standard Subroutine and Assign it to a Button
Manual Control Achieved: By assigning the macro to a button, you prevent Excel from lagging or freezing due to constant Worksheet_Change triggers. Your PivotTables will now only update exactly when you click the button.
Advanced PivotTables in WPS

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. 1. Open Your Macro-Enabled Workbook: Launch WPS Spreadsheet and open your existing .xlsm file containing the PivotTables.
  2. 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. 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.
Fully compatible with Microsoft Excel (.xlsm) macro-enabled workbooks.Familiar VBA environment for automating PivotTable filters and data refreshing.Lightweight and fast, even when handling large datasets with multiple PivotTables.Free to download and use with a highly intuitive tabbed interface.
microsoft office alternative - wps office

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.