Deleting Rows with a specific word

Guzzlr

Well-known Member
Joined
Apr 20, 2009
Messages
982
Office Version
  1. 2021
Platform
  1. Windows
HTML:
For Each sht In ActiveWorkbook.Worksheets
    If sht.Visible And (sht.Name = "Ramp") Then
        sht.Activate
    End If
    
    For r = Cells(Rows.Count, "M").End(xlUp).Row To 7 Step -1
        If Cells(r, "M").Value <> "Ramp" Then Rows(r).Delete
    Next r
End If

Hello All.
I'm trying to delete an entire row, if the word "Ramp" does not appear in column "M", beguinning on row 7...but I cant seem to get my End If and Next r in the proper place
Can someone lead me in the right direction please
Thank you
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
The final End If should be Next Sht
 
Upvote 0
Another way of doing it would be
Code:
Sub delramp()
   With Sheets("Ramp")
      With .Range("M7", .Range("M" & Rows.Count).End(xlUp))
         .AutoFilter 1, "<>Ramp"
         .Offset(1).SpecialCells(xlVisible).EntireRow.Delete
      End With
      .AutoFilterMode = False
   End With
End Sub
 
Upvote 0
Hi,
looking at posted code you are missing a Next sht

try this updated to your code

Code:
For Each sht In ActiveWorkbook.Worksheets
    If sht.Visible And UCase(sht.Name) = "RAMP" Then
    For r = sht.Cells(sht.Rows.Count, "M").End(xlUp).Row To 7 Step -1
        If UCase(sht.Cells(r, "M").Value) <> "RAMP" Then sht.Rows(r).Delete
    Next r
    End If
Next sht

your approach is not the quickest method but see if it works ok for you.

Dave
 
Upvote 0
Code:
For Each sht In ActiveWorkbook.Worksheets
    If sht.Visible And UCase(sht.Name) = "RAMP" Then
    For r = sht.Cells(sht.Rows.Count, "M").End(xlUp).Row To 7 Step -1
        If UCase(sht.Cells(r, "M").Value) <> "RAMP" Then sht.Rows(r).Delete
    Next r
    End If
Next sht

Queston, Why use "UCase" if the word is lower case "Ramp"?
thanks for the help
 
Upvote 0
Code:
For Each sht In ActiveWorkbook.Worksheets
If sht.Visible And UCase(sht.Name) = "RAMP" Then
    For r = sht.Cells(sht.Rows.Count, "M").End(xlUp).Row To 7 Step -1
        If UCase(sht.Cells(r, "M").Value) <> "RAMP" Then
        Intersect(Columns("M").SpecialCells(xlCellTypeBlanks).EntireRow, Columns("M:Q")).Delete xlUp
    Next r
End If
Next sht

So, getting back to this. I'm now trying to delete a range of columns M:Q, instead of the deleting the entire row.
I'm trying this code above, but having issues with it. Can someone lead me in the right direction please.
Thanks for the help
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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