VBA Delete all rows below if...

george hart

Board Regular
Joined
Dec 4, 2008
Messages
241
I need to delete all rows in a worksheet and the one/s including the name "Other Vehicles" or "Class 57". The problem I have is using something like the below doesn't help when theres caps/blanks cells between text???

In short, find "Other Vehicles" or "Class 57" and delete everything below it.


Cells.Find(What:="Other Vehicles", After:=ActiveCell, LookIn:=xlFormulas _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
Rows("24:55").Select
Selection.Delete Shift:=xlUp
Rows("23:23").Select
Selection.Delete Shift:=xlUp
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Where do you search for values? Entire worksheet?
 
Upvote 0
Hi

Thanks for your response...

I need to check these worksheets if that helps:

Tabs = Array("LAIRA STOP", "LANDORE", "SPM", "OOC STOP")
 
Upvote 0
Code:
[COLOR="Blue"]Sub[/COLOR] DeleteAllRows()

    [COLOR="Blue"]Dim[/COLOR] i [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Integer[/COLOR]
    [COLOR="Blue"]Dim[/COLOR] arrSheets [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Variant[/COLOR], cell [COLOR="Blue"]As[/COLOR] Range
    
    arrSheets = VBA.Array("LAIRA STOP", "LANDORE", "SPM", "OOC STOP")
    
    [COLOR="Blue"]For[/COLOR] i = 0 [COLOR="Blue"]To[/COLOR] [COLOR="Blue"]UBound[/COLOR](arrSheets)
        [COLOR="Blue"]With[/COLOR] Worksheets(arrSheets(i))
            [COLOR="Blue"]For[/COLOR] [COLOR="Blue"]Each[/COLOR] cell [COLOR="Blue"]In[/COLOR] .UsedRange
                [COLOR="Blue"]If[/COLOR] cell.Value [COLOR="Blue"]Like[/COLOR] "*" & "Other Vehicles" & "*" [COLOR="Blue"]Or[/COLOR] _
                   cell.Value [COLOR="Blue"]Like[/COLOR] "*" & "Class 57" & "*" [COLOR="Blue"]Then[/COLOR]
                   cell.EntireRow.Delete
                [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
            [COLOR="Blue"]Next[/COLOR]
        [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]With[/COLOR]
    [COLOR="Blue"]Next[/COLOR]

[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0
Many thanks

Your code works in that it finds and deletes the row if cells contains "Other Vehicles" or "Class 57", but it doesn't delete all the remaining rows below?? Bearing in mind, the cells/rows below may contain anything.

What I need is Find "Other Vehicles" or "Class 57" and delete that row and all the ones below regardless of what they contain.

Many thanks again for your with this...
 
Upvote 0
Code:
[COLOR="Blue"]Sub[/COLOR] DeleteAllRows()

    [COLOR="Blue"]Dim[/COLOR] i [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Integer[/COLOR]
    [COLOR="Blue"]Dim[/COLOR] arrSheets [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Variant[/COLOR], cell [COLOR="Blue"]As[/COLOR] Range
    
    arrSheets = VBA.Array("LAIRA STOP", "LANDORE", "SPM", "OOC STOP")
    
    [COLOR="Blue"]For[/COLOR] i = 0 [COLOR="Blue"]To[/COLOR] [COLOR="Blue"]UBound[/COLOR](arrSheets)
        [COLOR="Blue"]With[/COLOR] Worksheets(arrSheets(i))
            [COLOR="Blue"]For[/COLOR] [COLOR="Blue"]Each[/COLOR] cell [COLOR="Blue"]In[/COLOR] .UsedRange
                [COLOR="Blue"]If[/COLOR] cell.Value [COLOR="Blue"]Like[/COLOR] "*" & "Other Vehicles" & "*" [COLOR="Blue"]Or[/COLOR] _
                   cell.Value [COLOR="Blue"]Like[/COLOR] "*" & "Class 57" & "*" [COLOR="Blue"]Then[/COLOR]
                   .Range(.Cells(cell.Row, 1), .Cells(Rows.Count, 1)).EntireRow.Delete
                [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
            [COLOR="Blue"]Next[/COLOR]
        [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]With[/COLOR]
    [COLOR="Blue"]Next[/COLOR]

[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0
Thats great...works a treat.

Thanks very much. I was pulling my hair out last night trying to sort this...
 
Upvote 0
Hi again

Just one question. What code would I need should the tabs in question not exist.

For example if one was to delete on the tabs (or all of them) the code would fall over. I'd like it to just stop and issue a messege or something??

Any ideas??
 
Upvote 0
Code:
[COLOR="Blue"]Sub[/COLOR] DeleteAllRows()

    [COLOR="Blue"]Dim[/COLOR] i [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Integer[/COLOR]
    [COLOR="Blue"]Dim[/COLOR] arrSheets [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Variant[/COLOR], cell [COLOR="Blue"]As[/COLOR] Range
    
    arrSheets = VBA.Array("LAIRA STOP", "LANDORE", "SPM", "OOC STOP")
    
    [COLOR="Blue"]For[/COLOR] i = 0 [COLOR="Blue"]To[/COLOR] [COLOR="Blue"]UBound[/COLOR](arrSheets)
        [COLOR="Blue"]On[/COLOR] [COLOR="Blue"]Error[/COLOR] [COLOR="Blue"]Resume[/COLOR] [COLOR="Blue"]Next[/COLOR]
        [COLOR="Blue"]With[/COLOR] Worksheets(arrSheets(i))
            [COLOR="Blue"]For[/COLOR] [COLOR="Blue"]Each[/COLOR] cell [COLOR="Blue"]In[/COLOR] .UsedRange
                [COLOR="Blue"]If[/COLOR] cell.Value [COLOR="Blue"]Like[/COLOR] "*" & "Other Vehicles" & "*" [COLOR="Blue"]Or[/COLOR] _
                   cell.Value [COLOR="Blue"]Like[/COLOR] "*" & "Class 57" & "*" [COLOR="Blue"]Then[/COLOR]
                   .Range(.Cells(cell.Row, 1), .Cells(Rows.Count, 1)).EntireRow.Delete
                [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
            [COLOR="Blue"]Next[/COLOR]
        [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]With[/COLOR]
        [COLOR="Blue"]On[/COLOR] [COLOR="Blue"]Error[/COLOR] [COLOR="Blue"]GoTo[/COLOR] 0
    [COLOR="Blue"]Next[/COLOR]

[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,224,506
Messages
6,179,158
Members
452,892
Latest member
yadavagiri

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