How to Clear Excel Cells Automatically When Another Cell Changes
Question details
The user needs to automatically delete the contents of a specific range of cells (e.g., D2:F2) whenever the value of a control cell (e.g., G2) is changed.

- Product
- Microsoft Excel
- Device & OS
- Desktop (Windows/Mac)
- Scenario
- Managing data entry workflows where updating a status dropdown or control field must reset dependent input fields.
- Observed behavior
- Conditional formatting cannot delete cell data; a VBA Worksheet Change event macro is required to physically clear the cell contents.
Ensure you are using the desktop version of Excel, as VBA macros are not supported in Excel for the web or mobile apps. Save your current workbook before adding and executing new macro scripts.
Use a VBA Worksheet Change Event
Implement a VBA macro tied to the worksheet that listens for changes in your target cell and automatically clears the specified range.
Since conditional formatting only alters a cell's visual appearance and cannot delete data, you must use a VBA Worksheet Change event. This script will automatically trigger whenever data on the sheet is modified. By checking if the modified cell is your designated control cell, the script can clear the contents of other specific cells.
Press 'Alt + F11' on your keyboard to open the Visual Basic for Applications (VBA) editor in desktop Excel.
In the Project Explorer pane on the left, double-click the specific worksheet where you want this automation to occur (e.g., 'Sheet1').
Select 'Worksheet' from the left dropdown and 'Change' from the right dropdown at the top of the code window. Write logic using 'If Not Intersect(Target, Range("G2")) Is Nothing Then' to verify the changed cell. Inside this condition, check the cell's new value.
Before clearing the cells, add the line 'Application.EnableEvents = False'. Then add 'Range("D2:F2").ClearContents'. Finally, add 'Application.EnableEvents = True'. This prevents the clearing action from triggering the macro again in an infinite loop.
Close the VBA editor and go to File > Save As. Change the file format to 'Excel Macro-Enabled Workbook (*.xlsm)' to ensure your code is saved.

Automate Data Clearing with WPS Office
WPS Spreadsheet provides powerful and native support for VBA macros, allowing you to easily set up Worksheet Change events to clear dependent cells automatically. Enjoy full macro compatibility and a familiar interface in a lightweight application.
- 1. Enable Developer Tools: Open your spreadsheet in WPS Office, navigate to the 'Developer' tab, and click on 'VBA Editor'.
- 2. Apply the Worksheet Change Script: Double-click your target sheet in the Project window and paste your VBA script to clear contents upon cell change.
- 3. Save Macro Settings: Save your document as a Macro-Enabled Spreadsheet to retain the automated clearing functionality for future use.

Frequently Asked Questions
Can conditional formatting clear cell contents automatically?
No. Conditional formatting is exclusively designed to alter the visual appearance of cells (such as font colors, backgrounds, and borders) based on specific rules. It cannot delete, move, or modify the actual data within a cell.
Will this macro work if I share the file on Excel for the Web?
No, VBA macros are not supported in Excel for the Web, Android, or iOS. If a user opens the workbook in a browser, the VBA script will not execute, and the cells will not clear automatically. The file must be opened in the desktop app.
Why does my clear-cell macro crash Excel with an endless loop?
When your macro clears a cell, Excel registers this as a 'change' on the worksheet, which immediately triggers the macro to run again. To prevent this endless loop, you must include 'Application.EnableEvents = False' before the clearing command and set it back to 'True' afterward.




