Alternative to For Next Loop

smpatty08

Board Regular
Joined
May 16, 2014
Messages
155
I have this code;
Code:
For i = 2 To LastRow    If Cells(i, "D").Value = "" Then
        Range(Cells(i, "B"), Cells(i, "C")).ClearContents
    End If
Next i

What is the fastest way to get this done?

Thanks!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
If the cells are actually blank, rather than a formula showing "" try
Code:
    Range("D2", Range("D" & Rows.Count).End(xlUp)).SpecialCells(xlBlanks).Offset(, -2).Resize(, 2).Clear
 
Upvote 0
If the cells are actually blank, rather than a formula showing "" try
Code:
    Range("D2", Range("D" & Rows.Count).End(xlUp)).SpecialCells(xlBlanks).Offset(, -2).Resize(, 2).Clear[U][COLOR=#0000cd]Contents[/COLOR][/U]
You might also put in an ON ERROR RESUME NEXT just in case there aren't any blanks.
 
Upvote 0
You might also put in an ON ERROR RESUME NEXT just in case there aren't any blanks.

Excellent point Greg, this will now be
Code:
    On Error Resume Next
    Range("D2", Range("D" & Rows.Count).End(xlUp)).SpecialCells(xlBlanks).Offset(, -2).Resize(, 2).ClearContents
    On Error GoTo 0
 
Upvote 0
Excellent point Greg, this will now be
Code:
    On Error Resume Next
    Range("D2", Range("D" & Rows.Count).End(xlUp)).SpecialCells(xlBlanks).Offset(, -2).Resize(, 2).ClearContents
    On Error GoTo 0

I should have mentioned that the blank cells in Column "D" will not be contiguous. Is there a way to do this with Autofilter instead of For Next Loop?
 
Upvote 0
Are your cells actually blank or are they formulae returning ""
 
Upvote 0
This should be very fast - assumes data in Sheet1

Code:
Sub aTest()
    Dim LastRow As Long
    
    With Sheets("Sheet1")
        LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
        .Range("B2:C" & LastRow).Value = _
            .Evaluate("=IF(D2:D" & LastRow & "="""","""",C2:B" & LastRow & ")")
    End With
End Sub

M.
 
Upvote 0
I should have mentioned that the blank cells in Column "D" will not be contiguous. Is there a way to do this with Autofilter instead of For Next Loop?
Fluff's (and Marcelo's for that matter) should work on non-contiguous ranges. You wouldn't need autofilter in order to pull this off.
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top