How to Clear Excel Cells When ID and Category Match Using VBA
Question details
The user needs to automate the process of clearing specific cell contents when a unique ID in column A and a category header in another column match a predetermined list of invalid data combinations.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating data cleaning by matching unique IDs and category headers against an external list of invalid combinations, and troubleshooting a VBA macro error that occurred after changing column references.
- Observed behavior
- Conditional formatting was considered but cannot delete data. A VBA macro was deployed but threw a Run-time error 1004 when the column references were modified.
Before running any VBA macro that modifies or deletes data, always create and save a backup copy of your worksheet, as actions performed by macros generally cannot be undone.
Use a VBA Macro to Find and Clear Matching Cells
Since formulas and conditional formatting cannot delete cell contents, a VBA script is required to locate intersecting data and clear it automatically.
A VBA macro can be programmed to loop through your external list of invalid combinations, search column A for the matching ID, find the corresponding category header, and clear the intersecting cell.
Press Alt + F11 on your keyboard to open the Microsoft Visual Basic for Applications window.
Click 'Insert' from the top menu and select 'Module' to create a blank script window.
Enter your VBA script designed to build a range of affected cells by matching Column A (IDs) and row headers (Categories) against your external invalid data list.
Press F5 or click the 'Run' button (the green triangle) to execute the code and clear the targeted cell contents.

Troubleshoot VBA Run-time Error 1004
Run-time error 1004 commonly occurs when column references are changed incorrectly or when a specified range cannot be found.
Use Conditional Formatting for Manual Deletion
If you are not comfortable modifying VBA code, you can use Conditional Formatting to highlight the invalid cells and then manually delete their contents.
Automate Data Cleaning Seamlessly in WPS Spreadsheet
WPS Spreadsheet provides powerful data processing capabilities, including robust support for VBA macros in advanced versions. You can easily run your automated cleaning scripts, troubleshoot errors, and manage large datasets efficiently.
- 1. Open Your Workbook: Launch WPS Spreadsheet and open your existing .xlsm file containing the invalid data list.
- 2. Access the Developer Tools: Navigate to the Developer tab on the ribbon to access your macro and VBA options.
- 3. Run or Debug Your Macro: Click 'Macros' to run your cell-clearing script, or open the 'VBA Editor' to fix any reference errors quickly.

Frequently Asked Questions
Can conditional formatting delete cell contents automatically?
No. Conditional formatting is strictly a visual tool used to change the appearance of cells (such as background or font color) based on a formula or rule. It cannot alter or delete the actual data inside the cell; you must use VBA for automatic deletion.
Why do I get Run-time error 1004 when changing column letters in VBA?
Run-time error 1004 usually indicates an application-defined or object-defined error. It frequently happens if you change column letters in the code but reference a range that doesn't exist, is protected, or is formatted improperly. Clicking 'Debug' will highlight the exact line causing the issue.
How can I undo a VBA macro if it clears the wrong cells?
Standard Undo functions (Ctrl + Z) generally do not work for actions performed by a VBA macro. This is why it is highly recommended to test macros on a duplicate copy of your worksheet or ensure you have a recently saved backup before running the script.
Is there a non-macro way to delete matching data quickly?
While not fully automated, you can add a helper column using functions like INDEX and MATCH or XLOOKUP to flag rows containing invalid combinations. You can then filter the dataset by these flags, select the visible cells, and press the Delete key.




