Non VBA Way
you can put formula =IF(LEN(TRIM(C2))=0,"",POPULATETHISCELL)
in the cells A
and B
where you can replace POPULATETHISCELL with whatever you are trying to get there. For example, populating today's date.
VBA Way
- Work with
Objects
. Avoid the use of ActiveWorkbook/Activesheet/Selection
etc as show HERE
- Avoid the use of
UsedRange
. Find last row as shown HERE and then loop through it.
- Instead of clearing ranges twice, you can clear it in one go as shown below
Is this what you are trying?
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim lastrow As Long
Dim i As Long
Set ws = ThisWorkbook.Sheets("Sold")
With ws
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
lastrow = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
For i = 2 To lastrow
If Len(Trim(.Range("C" & i).Value)) = 0 Then _
.Range("A" & i & ":B" & i).ClearContents
Next i
End If
End With
End Sub