How to Clear Specific Cells on the Active Excel Row Using VBA
Question details
The user needs a VBA macro that clears specific cells within the currently active row instead of deleting the entire row when a checkbox is selected.

- Product
- Excel / WPS Spreadsheet
- Device & OS
- not provided
- Scenario
- Modifying an existing macro triggered by a checkbox that currently deletes an entire row.
- Observed behavior
- The current macro deletes the whole row, but the required behavior is to only empty the contents of a specified cell range within that active row.
Before running or editing any VBA macros, make sure to save a backup copy of your workbook. Enable the Developer tab in your spreadsheet software to access the Visual Basic Editor easily.
Use the Active Row Number to Clear a Target Range
Identify the current active row and clear the exact column range you need using the ClearContents method. This is the safest and most precise approach.
Instead of targeting the entire row for deletion, you can specify exactly which columns to clear by concatenating column letters with the active row's index number.
Press ALT + F11 to open the VBA editor and locate the module or sheet containing your checkbox macro.
Remove the code that deletes the row (e.g., ActiveCell.EntireRow.Delete) and add a variable to store the row number: thisRow = ActiveCell.Row
Use the ClearContents method on the target columns. For example, to clear columns J through BZ, add: Range("J" & thisRow & ":BZ" & thisRow).ClearContents
Replace "J" and "BZ" in the code with the specific starting and ending column letters you want to clear.

Use Selection.Resize to Clear Cells Relative to Selection
If you want to clear a specific number of cells starting directly from the currently selected cell, you can use the Resize property.
Write and Execute VBA Macros Seamlessly with WPS Office
WPS Spreadsheet fully supports VBA and Excel macros, allowing you to easily run and edit scripts to clear cells, automate tasks, and manipulate active rows just like you would in Microsoft Excel.
- 1. Open Your Spreadsheet: Launch WPS Office and open your macro-enabled spreadsheet.
- 2. Access the Developer Tab: Go to the 'Developer' tab on the top ribbon and click 'Visual Basic Editor'.
- 3. Insert VBA Code: Paste your ClearContents VBA code into the relevant module.
- 4. Assign to Checkbox: Right-click your spreadsheet checkbox, select 'Assign Macro', and choose your newly modified code.

Frequently Asked Questions
What is the difference between ClearContents and Delete in VBA?
The ClearContents method only removes the data (text, numbers, or formulas) inside the cells, leaving the cell formatting and the row itself intact. The Delete method removes the cells entirely, shifting surrounding cells up or to the left, which alters the layout of your worksheet.
How do I clear cells without removing their background color or borders?
By using the Range.ClearContents method in VBA, you only erase the values within the cells while preserving all formatting. If you wanted to remove the formatting as well, you would use Range.Clear. To clear only formats but keep the text, use Range.ClearFormats.
Can I run this VBA code across multiple selected rows at once?
Yes, but you will need to modify the VBA code to loop through each row in the current Selection, or use a single intersecting range reference that encompasses all selected rows, such as Intersect(Selection.EntireRow, Range("J:BZ")).ClearContents.




