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

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Nope, that done the same.

I think it is looking for bus in lower case when the word could be in all case sizes as stated in an earlier post. I do know in this particular case they are all upper case hence deleting everything. How do you amend the code to cater for all case sizes?
 
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
 
Upvote 0
I think it is looking for bus in lower case when the word could be in all case sizes as stated in an earlier post. I do know in this particular case they are all upper case hence deleting everything. How do you amend the code to cater for all case sizes?

Please?
 
Upvote 0
My results with all cells in capitals

Excel Workbook
R
1A BUS
2BUS
3BUSTED
4BUSES
Sheet3
 
Upvote 0
Doesnt the Lcase look for lower case only? I changed the code to Ucase and it left what I needed. But in some cases the word will be in a mixture of cases.
 
Upvote 0
No, it is a case-insensitive comparison. It is looking for bus in the cell's value converted to lower case. If it isn't found then the row is deleted.
 
Upvote 0
So if BUS is in upper case it changes it to lower case then deletes all the other rows.
 
Upvote 0
It doesn't change the value in the cell. In code it uses LCase to convert to lower case (in memory, not on the sheet) then uses InStr to see if bus is present.
 
Upvote 0

Forum statistics

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