VBA & Macro Problems

How to Clear Specific Cells on the Active Excel Row Using VBA

Chanuka GeekiyanageChanuka Geekiyanage Nov 4, 2026 870 views

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.

How to Clear Specific Cells on the Active Excel Row Using VBA
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 you start

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.

Solution 1Recommended

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.

1
Open the Visual Basic Editor

Press ALT + F11 to open the VBA editor and locate the module or sheet containing your checkbox macro.

2
Define the Active Row

Remove the code that deletes the row (e.g., ActiveCell.EntireRow.Delete) and add a variable to store the row number: thisRow = ActiveCell.Row

3
Clear the Specific Range

Use the ClearContents method on the target columns. For example, to clear columns J through BZ, add: Range("J" & thisRow & ":BZ" & thisRow).ClearContents

4
Adjust to Your Columns

Replace "J" and "BZ" in the code with the specific starting and ending column letters you want to clear.

Use the Active Row Number to Clear a Target Range
Advanced Macros in WPS Spreadsheet

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. 1. Open Your Spreadsheet: Launch WPS Office and open your macro-enabled spreadsheet.
  2. 2. Access the Developer Tab: Go to the 'Developer' tab on the top ribbon and click 'Visual Basic Editor'.
  3. 3. Insert VBA Code: Paste your ClearContents VBA code into the relevant module.
  4. 4. Assign to Checkbox: Right-click your spreadsheet checkbox, select 'Assign Macro', and choose your newly modified code.
Highly compatible with Microsoft Excel (.xlsm, .xlsx) files and VBA macros.Built-in macro editor for writing, testing, and debugging VBA scripts.Lightweight, fast performance, and completely free to download.Familiar ribbon interface makes it easy to find Developer tools and assign macros to checkboxes.
microsoft office alternative - wps office

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.