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
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
You need to loop backwards when deleting rows. Try

Code:
For B = A To 1 Step -1
If LCase(Selection.Value) Like "* bus *" Then
 
Upvote 0
You need to loop backwards when deleting rows. Try

Code:
For B = A To 1 Step -1
If LCase(Selection.Value) Like "* bus *" Then

Thanks but that didnt delete anything.
 
Upvote 0
Then maybe without the spaces but this would also delete busted for example

Code:
For B = A To 1 Step -1
If LCase(Selection.Value) Like "*bus*" Then
 
Upvote 0
Then maybe without the spaces but this would also delete busted for example

Code:
For B = A To 1 Step -1
If LCase(Selection.Value) Like "*bus*" Then

Thanks that worked, luckily I havent got the word busted! May I ask why I need to loop backwards when deleting rows please.
 
Upvote 0
Imagine that rows 1 and 2 both contain bus. Loop forwards and you delete row 1. Row 2 becomes row 1 but your loop then looks at row 2 (which is now the old row 3) and misses row 1 (the old row 2).

If you loop backwards that doesn't happen.
 
Upvote 0
Thanks, could I ask another thing. Would the code change drastically if I wanted to delete all the rows that didnt have the word 'Bus'?
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,841
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