VBA to delete rows between two specific strings in the same column

DerekExcels

New Member
Joined
Aug 18, 2023
Messages
2
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hey,

Hope everyone is well. Please see below example. I need a vba that deletes all rows between "Car" and "Bus" but I want all the rows with "Car" and "Bus" to remain and everything else to stay as is, i.e. "Bike" and "Jet".

With the below example, (Blanks) and "Truck" will be deleted with vba.

Column A

Bike
Bike
(Blank)
Car
Car
Car
(Blank)
Truck
(Blank)
Bus
Bus
Bus
(Blank)
Jet

Really appreciated,
Derek
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Welcome to the MrExcel board!

Assuming at least one "car" and one "bus" in column A, try this with a copy of your workbook.

VBA Code:
Sub Delete_Rows()
  Dim fr As Long, lr As Long

  fr = Columns("A").Find(What:="Car", LookAt:=xlWhole, SearchDirection:=xlPrevious, MatchCase:=False).Row + 1
  lr = Columns("A").Find(What:="Bus", LookAt:=xlWhole, SearchDirection:=xlNext, MatchCase:=False).Row - 1
  If lr >= fr Then Rows(fr).Resize(lr - fr + 1).Delete
End Sub
 
Upvote 0
Solution
Hey Peter

You saved my day and now my work can be a bit easier. Exactly the VBA I was looking for.

Really thankful.
 
Upvote 0
You are welcome. Glad to help. Thanks for the follow-up. :)
 
Upvote 0
Welcome to the MrExcel board!

Assuming at least one "car" and one "bus" in column A, try this with a copy of your workbook.

VBA Code:
Sub Delete_Rows()
  Dim fr As Long, lr As Long

  fr = Columns("A").Find(What:="Car", LookAt:=xlWhole, SearchDirection:=xlPrevious, MatchCase:=False).Row + 1
  lr = Columns("A").Find(What:="Bus", LookAt:=xlWhole, SearchDirection:=xlNext, MatchCase:=False).Row - 1
  If lr >= fr Then Rows(fr).Resize(lr - fr + 1).Delete
End Sub
Peter_SSs:

I have the same issue, but I have a column full of "car" & "bus", how would I have it repeat that VBA for the entire A column?

Thanks, Dean
 
Upvote 0
Peter_SSs:

I have the same issue, but I have a column full of "car" & "bus", how would I have it repeat that VBA for the entire A column?

Thanks, Dean
Could you give a small but representative set of sample data with 'before' and 'after' details (preferably with XL2BB)?
 
Upvote 0
Peter_SSs,

Thank you for reaching out, but I was able to find something that worked for this problem. Again, Thank you.
 
Upvote 0

Forum statistics

Threads
1,215,139
Messages
6,123,263
Members
449,093
Latest member
Vincent Khandagale

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