VBA & Macro Problems

Clear a Range of Cells in an Excel VBA For Loop

Aamir Naveed AkramAamir Naveed Akram Dec 2, 2026 868 views

Question details

Clear selected cells within rows using a VBA For loop without deleting the entire rows.

Clear a Range of Cells in an Excel VBA For Loop
Product
Microsoft Excel
Device & OS
not provided
Scenario
A VBA macro processes rows and needs to clear columns A through G on a selected row.
Observed behavior
Only the specified cells should be cleared, while the worksheet rows and other columns remain intact.
Before you start

Identify the worksheet, columns, and row numbers you want the macro to affect. Check whether your loop variable represents the target row or the row immediately above it.

Solution 1Recommended

Use ClearContents on a row-specific range

Build an A:G range from the loop's row number to clear cell contents without deleting the worksheet row.

ClearContents removes values and formulas but leaves cell formatting and the worksheet rows in place. The example clears A2:G11; it does not identify duplicate rows.

1
Open the VBA editor

In Excel, open the Developer tab and select Visual Basic. In the VBA editor, choose Insert > Module.

2
Add the loop

Paste this example into the module, replacing Sheet1 with your worksheet name: Sub ClearSelectedCells() Dim r As Long Dim ws As Worksheet Set ws = ThisWorkbook.Worksheets("Sheet1") For r = 1 To 10 ws.Range("A" & (r + 1) & ":G" & (r + 1)).ClearContents Next r End Sub

3
Adapt and run the macro

Change the loop bounds and column letters to match your data. If your existing loop identifies duplicates, place the ClearContents statement inside its duplicate condition. Run the macro from Developer > Macros.

Use ClearContents on a row-specific range
Check the row offset: The expression r + 1 targets the row after r. If r is already the row to clear, use ws.Range("A" & r & ":G" & r).ClearContents instead.
Free Microsoft Office alternative

Manage spreadsheet ranges with WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and Microsoft Office format compatibility for an easy move between applications. Excel VBA behavior and macro support may vary by WPS version and configuration.

Select and clear cell contents without deleting entire rowsOpen and edit common Microsoft Excel formats, including XLSX and XLSUse a familiar spreadsheet interface when moving from ExcelWork with a free, lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

Does ClearContents delete the row or its formatting?

No. Range.ClearContents removes values and formulas from the specified cells, but it does not delete the row or remove cell formatting.

How do I clear the current loop row instead of the next row?

Use ws.Range("A" & r & ":G" & r).ClearContents inside the loop. The r + 1 expression targets the following row.

Will the example find duplicate rows?

No. The example clears A2:G11. To clear only duplicates, put the ClearContents statement inside the condition in your existing loop that identifies a duplicate.

How can I clear a different set of columns?

Change the column letters in the Range address. For example, ws.Range("B" & r & ":D" & r).ClearContents clears columns B through D on row r.