How to Clear an Excel Cell When Two Cells Match Using VBA
Question details
The user needs a method to automatically clear the contents of a specific cell (such as A1) if its value matches another designated cell (such as B1), and wishes to apply this condition across single rows or a large dataset.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating data cleanup by removing duplicate or matched values across specific columns without manual deletion.
- Observed behavior
- Because standard Excel formulas cannot delete cell contents or manipulate data directly, a VBA macro is required to execute the active clearing of matched cell data.
Before running any macros, save your workbook as a Macro-Enabled Workbook (.xlsm) and create a backup of your data, as VBA deletion actions usually cannot be reversed with the Undo button.
Clear a Single Cell When It Matches Another Using VBA
Use a basic VBA macro to compare two specific cells (e.g., A1 and B1) and clear the first cell if their values are identical.
Standard Excel formulas can only calculate and return values; they cannot manipulate or delete data in other cells. To actively clear a cell based on a condition, a VBA macro must be used.
Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor in Excel.
Click 'Insert' in the top menu bar and select 'Module' to create a blank script window.
Copy and paste the following code into the module window: Sub Match_A1_B1() If ActiveSheet.Range("A1").Value = ActiveSheet.Range("B1").Value Then ActiveSheet.Range("A1").ClearContents End If End Sub
Close the VBA editor to return to your worksheet. Press ALT + F8, select 'Match_A1_B1' from the list of macros, and click 'Run'.

Clear Matching Cells Across Multiple Rows
Use a loop macro to compare columns row by row and clear the cell in column A if its value matches the adjacent cell in column B.
Run Macros and Automate Spreadsheets in WPS Office
WPS Spreadsheet features comprehensive support for VBA macros, allowing you to run custom automation scripts like clearing matched cells effortlessly. It offers a familiar interface and seamless compatibility with your existing Excel macro-enabled workbooks.
- 1. Download and Install WPS Office: Download the free WPS Office suite from the official website and install it on your device.
- 2. Open Your Macro-Enabled File: Launch WPS Spreadsheet and open your existing Excel workbook containing the dataset.
- 3. Enable Macros and Run: Navigate to the Developer tab, enable macros if prompted, and click 'Macros' to run your cell-clearing script just as you would in Microsoft Excel.

Frequently Asked Questions
Can I use a standard Excel formula instead of VBA to clear a cell?
No, standard Excel formulas can only calculate and display a value in the cell they reside in. They cannot delete data, change formatting, or actively manipulate other cells. A VBA macro is strictly required to clear contents automatically.
How do I make the VBA cell comparison case-insensitive?
You can wrap your cell values in the `UCase()` function within your VBA code (e.g., `UCase(Range("A1").Value) = UCase(Range("B1").Value)`). This converts both text strings to uppercase before making the comparison, effectively treating 'Text' and 'text' as a perfect match.
How do I clear the entire row if two cells match instead of just one cell?
You can modify the action line in your looping VBA code from `Cells(i, "A").ClearContents` to `Rows(i).ClearContents`. If you want to delete the row completely instead of just emptying it, use `Rows(i).Delete`.
Why isn't my macro running when I press ALT + F8?
Ensure that your macro security settings allow code execution. Go to the Developer tab, click Macro Security, and enable macros. Additionally, verify that you have saved your workbook in a macro-supporting format like .xlsm.




