I have an excel macro to do the following
For Column O, for each cell in Column O, if cell is blank, then for that row, clear all cells going right.
For example, if Cell O7 is blank, it will clear all the contents of all the Cells from P7,Q7,R7, S7 etc. onwards. But leaving the cells on the left (A7 to N7) untouched.
The macro below works, but after encountering a blank cell (O7), then there is values in O8 onwards, but the macro doesnt seem to go through to the bottom of the sheet? How would I fix this? Most appreciated, thanks.
For Column O, for each cell in Column O, if cell is blank, then for that row, clear all cells going right.
For example, if Cell O7 is blank, it will clear all the contents of all the Cells from P7,Q7,R7, S7 etc. onwards. But leaving the cells on the left (A7 to N7) untouched.
The macro below works, but after encountering a blank cell (O7), then there is values in O8 onwards, but the macro doesnt seem to go through to the bottom of the sheet? How would I fix this? Most appreciated, thanks.
VBA Code:
Sub ClearCellsIfBlank()
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long, j As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
lastRow = ws.Cells(ws.Rows.Count, "O").End(xlUp).Row
For i = 1 To lastRow
If IsEmpty(ws.Cells(i, "O")) Then
For j = i To ws.Cells(i, ws.Columns.Count).End(xlToLeft).Column
ws.Cells(i, j).ClearContents
Next j
End If
Next i
End Sub