Help With Delete Row Code

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,786
Office Version
  1. 365
Platform
  1. Windows
I am a novice when writing code but have written the code below that looks for the word 'Bus' (as an example) in a cell and delete the entire row. How do I change the code so that it deletes the row if the word 'Bus' is amongst other data in the cell and not on its own. Thanks. Also I want the code to look for all case types (BUS, bus, Bus)

Code:
Sub Delete()
Application.ScreenUpdating = False
Dim A As Long
Dim B As Long
Range("R1").Select
A = Selection.CurrentRegion.Rows.Count
For B = 1 To A
If Selection.Value = "Bus" Then 'Change word when necessary
Selection.EntireRow.Delete
Else
Selection.Offset(1, 0).Select
End If
Next B
Application.ScreenUpdating = True
End Sub
 
Hi again VoG, how do I amend the code to look for more than one word at a time e.g Bus & Van? Thanks
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Try this - if either bus or van is found the row won't be deleted.

Code:
Sub Delete()
Application.ScreenUpdating = False
Dim A As Long
Dim B As Long
Range("R1").Select
A = Selection.CurrentRegion.Rows.Count
For B = A To 1 Step -1
If InStr(LCase(Selection.Value), "bus") = 0 And InStr(LCase(Selection.Value), "van") = 0 Then
Selection.EntireRow.Delete
Else
Selection.Offset(1, 0).Select
End If
Next B
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Before

Excel Workbook
R
1a bus
2bus
3busted
4a bus
5s
6d
7busty
8buses
Sheet3




Code:
Sub Delete()
Application.ScreenUpdating = False
Dim A As Long
Dim B As Long
Range("R1").Select
A = Selection.CurrentRegion.Rows.Count
For B = A To 1 Step -1
If InStr(LCase(Selection.Value), "bus") = 0 Then
Selection.EntireRow.Delete
Else
Selection.Offset(1, 0).Select
End If
Next B
Application.ScreenUpdating = True
End Sub

After

Excel Workbook
R
1a bus
2bus
3busted
4a bus
5busty
6buses
7
8
9
10
11
Sheet3


Hi VoG all of a sudden this is bugging out and pointing to :-

Selection.EntireRow.Delete

Why would this be? It has got 300,000 rows to look at would that be it for some reason?
 
Upvote 0
I don't know why that would error but this should be faster with 300,000 rows

Code:
Sub DeleteMe()
Dim A As Long
Dim B As Long
Application.ScreenUpdating = False
A = Range("A" & Rows.Count).End(xlUp).Row
For B = A To 1 Step -1
    If InStr(LCase(Range("R" & B).Value), "bus") = 0 Then Rows(B).Delete
Next B
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Tried it and it just froze and had to esc after about 45mins.
 
Upvote 0
Try this

Code:
Sub Dazza()
Dim r As Range, LR As Long
Application.ScreenUpdating = False
LR = Range("R" & Rows.Count).End(xlUp).Row
Set r = Cells(2, 1).Resize(LR - 1, 1)
Cells(1, 18).AutoFilter field:=1, Criteria1:="<>*bus*"
r.SpecialCells(xlCellTypeVisible).EntireRow.Delete
Cells(1, 18).AutoFilter
Application.ScreenUpdating = True
End Sub
 
Upvote 0
That deleted every row. Remember the word bus will be amongst other text as well as on its own in a cell. (Which is why I cant do sort then delete).
 
Upvote 0
For me it only deleted rows where bus did not appear anywhere in column R of the row.

That is because of this

Rich (BB code):
Cells(1, 18).AutoFilter field:=1, Criteria1:="<>*bus*"
 
Upvote 0
I dont know where to go from here then if it works for you!
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,848
Members
452,948
Latest member
UsmanAli786

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