Clear a Range of Cells in an Excel VBA For Loop
Question details
Clear selected cells within rows using a VBA For loop without deleting the entire rows.

- 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.
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.
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.
In Excel, open the Developer tab and select Visual Basic. In the VBA editor, choose Insert > Module.
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
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.

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.

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.




