Looking for help with a macro to delete a row

jnewton34120

New Member
Joined
Feb 2, 2024
Messages
9
Office Version
  1. 365
Platform
  1. Windows
I am looking to see if anyone can help me with a macro. I am looking to have one that looks at column H and if the code in the column starts with a 5 or 6 then it should keep it and if there is anything else in the column the entire row should be deleted. I don't currently know the number of rows that will have data but I can get that information if needed.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Try this:
VBA Code:
Sub MyDeleteRows()

    Dim r As Long
    
    Application.ScreenUpdating = False
    
'   Loop through all rows in column H with data backwards, starting at bottom
    For r = Cells(Rows.Count, "H").End(xlUp).Row To 1 Step -1
'       Delete row if cell starts with a 5 or 6
        If Left(Cells(r, "H"), 1) = "5" Or Left(Cells(r, "H"), 1) = "6" Then Rows(r).Delete
    Next r
        
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
I was looking for it to not delete the rows starting in 5 or 6 is that something that is possible please? It wouldn't matter what it is a long as it doesn't start with a 5 or 6 it can be deleted.
 
Upvote 0
Sorry, misread that. Simple change:
VBA Code:
Sub MyDeleteRows()

    Dim r As Long
    
    Application.ScreenUpdating = False
    
'   Loop through all rows in column H with data backwards, starting at bottom
    For r = Cells(Rows.Count, "H").End(xlUp).Row To 1 Step -1
'       Delete row if cell starts with a 5 or 6
        If Left(Cells(r, "H"), 1) <> "5" And Left(Cells(r, "H"), 1) <> "6" 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,224,745
Messages
6,180,699
Members
452,994
Latest member
Janick

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