Deleting Rows VBA

AgustinGuerrero

New Member
Joined
May 8, 2022
Messages
3
Office Version
  1. 2016
Hi, I want to create a coda that allows me to find a specific text on a range and then select all the rows until it finds another value and delete all the rows
For example, I want to search the word "Hello" in Range ("A:A") and then delete all the rows (the one with Hello text as well) until it finds the word "Goodbye".
I started with Range("A:A").Find("Hello").select but don't know how to select all the rows up to "goodbye" and delete them.
Many thanks!


1652031371866.png
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
VBA Code:
Sub Test()

myRange = Cells(Rows.Count, "A").End(xlUp).Row

For i = 1 To myRange
    If InStr(1, Range("A" & i).Value, "Hello") Then
        myStart = i
    End If
   
    If InStr(1, Range("A" & i).Value, "Goodbye") Then
        myEnd = i
    End If
Next i

' Delete Hello through Goodbye including both Hello and Goodbye
Range("A" & myStart & ":A" & myEnd).Delete shift:=xlShiftUp

' Delete Hello through Goodbye excluding Goodbye
Range("A" & myStart & ":A" & myEnd - 1).Delete shift:=xlShiftUp


End Sub
 
Upvote 0
This code will fetch the two row numbers, then you can use them to delete how you need:
VBA Code:
Dim srchF, srchL
Set srchF = Range("A:A").Find(What:="Hello", After:=Range("A1"), LookAt:=xlWhole)
Set srchL = Range("A:A").Find(What:="Goodbye", After:=Range("A" & srchF.Row), LookAt:=xlWhole)
MsgBox srchF.Row & " , " & srchL.Row
PS. Hi to all.
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,384
Members
449,080
Latest member
Armadillos

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