Delete Rows with contents starting with 1 via Visual Basic

StuartM1

Board Regular
Joined
Oct 6, 2010
Messages
115
Hi,
I'm trying to teach myself macros. :cool:😵
One of the first steps I want to record is to delete rows based on Column C containing text starting with a '1'.
I found the code below from the interwebs.
I have no idea what Columns i and j have to do with the goal.
Could someone suggest something else or explain why the below fails?
Thanks very much as always!!!!!

VBA Code:
Sub Delete_Rows_Starting_with_1()
For i = 1 To Selection.Rows.Count
For j = 1 To Selection.Rows.Count
If Left(Selection.Cells(j, 1), 1) = "1" Then
Rows(j + 3).EntireRow.Delete
End If
Next j
Next i
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
You need to loop through the rows backwards if you are deleting rows, or else some might get missed.

Try this:
VBA Code:
Sub Delete_Rows_Starting_with_1()

    Dim lr As Long
    Dim r As Long
   
    Application.ScreenUpdating = False
   
'   Find last row in column C with data
    lr = Cells(Rows.Count, "C").End(xlUp).Row
   
'   Loop through rows backwards
    For r = lr To 1 Step -1
'       Delete row if it starts with a "1"
        If Left(Cells(r, "C"), 1) = "1" Then Rows(r).Delete
    Next r
   
    Application.ScreenUpdating = True
   
End Sub
 
Upvote 0
Solution
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,223,882
Messages
6,175,166
Members
452,615
Latest member
bogeys2birdies

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