How to Run One of Two Excel Macros When a Cell Value Changes
Question details
The user wants to assign two different macro actions (LS_FilterData and LS_ClearFilter) to a single cell (D4), triggering the appropriate macro when the cell's value changes, and then automatically switching the cell's text to the alternative command.
- Product
- Excel
- Device & OS
- not provided
- Scenario
- Creating a dynamic toggle cell to automate filtering actions, where changing the cell text applies or clears a data filter.
- Observed behavior
- Currently, the two macros are assigned individually to different cells. The goal state is to consolidate them into one interactive cell that toggles its function and display text upon execution.
Ensure that your workbook is saved as a Macro-Enabled Workbook (.xlsm) and that macros are enabled in your Trust Center settings so that event-based VBA code can execute.
Use the Worksheet_Change Event to Toggle Macros
Utilize the Worksheet_Change event in VBA to monitor a specific cell. Depending on the text in the cell, trigger the appropriate macro and then update the cell's text to the alternative command.
By placing a Worksheet_Change event in the specific sheet module, Excel can listen for edits in your target cell (e.g., D4). When the user changes the text to 'FILTER DATA', the code runs your filter macro and rewrites the cell to 'CLEAR FILTER', effectively creating a toggle switch.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) Editor in Excel.
In the Project Explorer pane on the left, double-click the specific worksheet name (e.g., Sheet1) where cell D4 is located. Do not place this code in a standard Module.
Select 'Worksheet' from the left dropdown at the top of the code window, and 'Change' from the right dropdown. This creates the Worksheet_Change subroutine.
Inside the subroutine, write an If statement to check if 'Target.Address = "$D$4"'. If true, check the value of Target.Value. If it equals 'FILTER DATA', call your 'LS_FilterData' macro. If it equals 'CLEAR FILTER', call 'LS_ClearFilter'.
Before changing the cell text within the code, add 'Application.EnableEvents = False'. Then change D4 to the opposite command. Finally, restore events with 'Application.EnableEvents = True'.
Automate Tasks with Macros in WPS Spreadsheet
WPS Office fully supports VBA macros, allowing you to seamlessly run Worksheet_Change events, automate data filtering, and create interactive toggle cells just like in Microsoft Excel.
- 1. Open your Workbook in WPS: Launch WPS Spreadsheet and open your .xlsm macro-enabled file.
- 2. Access the VBA Editor: Navigate to the Developer tab on the ribbon and click 'Visual Basic', or press Alt + F11.
- 3. Add the Worksheet_Change Code: Double-click your target sheet in the Project window and paste your toggle macro logic into the code area.
- 4. Save and Test: Save the workbook, return to the spreadsheet, and change the value in your target cell to trigger the automated toggle.

Frequently Asked Questions
Why does my Worksheet_Change macro crash Excel in an infinite loop?
This usually happens if your macro changes a cell value without turning off events first. The change made by the macro triggers the event again. Use Application.EnableEvents = False before making changes to the sheet, and Application.EnableEvents = True immediately after.
Can I use a clickable shape or button instead of a cell value change to toggle macros?
Yes. You can insert a shape, Form Control, or ActiveX button, assign a single toggle macro to it, and have the macro check a hidden cell's status or the button's own text (e.g., ActiveSheet.Shapes("Button 1").TextFrame.Characters.Text) to decide which action to perform.
Does WPS Spreadsheet support the Worksheet_Change event?
Yes, WPS Spreadsheet fully supports VBA and standard Excel events, including Worksheet_Change, allowing you to run your existing macro scripts without modification.




