Delete rows between two texts in ColumnA

nevsky092315

New Member
Joined
May 3, 2020
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am trying to build a macro for the following condition:

Find the cell in column A that has Text A and delete all rows until that cell and then find Text B and delete all rows until the next Text A
and again find the next Text B and so on... this loop needs to repeat for minimum of 4 iterations to maximum of 12 iterations.

Sub FindTextAB()
Find_TextA
Find_TextB
End Sub
--------------------------------------------
Sub Find_TextA()
Do Until ActiveCell.Value = "TextA"
Selection.EntireRow.Delete --->This loop is where I am facing error, macro freezes and it doesn't come out of the loop once there is no data in below cells)
Loop
End Sub
----------------------------------------------
Sub Find_TextB()

Cells.Find(What:="TextB", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
End Sub
----------------------------------------------

Your help is greatly appreciated.

Thank you,
Nevsky
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

igold

Well-known Member
Joined
Jul 8, 2014
Messages
2,551
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
I only looked at your code long enough to tell you that if you are deleting rows, always start at the bottom and work you way up.
 

nevsky092315

New Member
Joined
May 3, 2020
Messages
7
Office Version
  1. 365
Platform
  1. Windows
@igold Thanks for the input....I wish I knew that method....new to VBA, beginner level.
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,203
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
Maybe this
VBA Code:
Sub MM1()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 2 Step -1
    If Cells(r, 1).Value <> "Text A" And Cells(r, 1).Value <> "Text B" Then Rows(r).Delete
Next r
End Sub
 

nevsky092315

New Member
Joined
May 3, 2020
Messages
7
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Hi Michael, Thanks for taking your time....the code deletes all rows b/w both the text...sorry may be I should have put my logic more clearer.... I need the data b/w "TextA" to "TextB" to remain and data b/w "TextB" to "TextA" get deleted.
 

igold

Well-known Member
Joined
Jul 8, 2014
Messages
2,551
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
A little convoluted but how about this:

VBA Code:
Sub RemoveText()

    Dim lRow As Long, i As Long, x As Long
    Dim rng As Range
    
Cycle:
    lRow = Cells(Rows.Count, "A").End(xlUp).Row - x
    Set rng = Range("A1:A" & lRow)
    For i = rng.Rows.Count To 2 Step -1
        If rng.Cells(i) = "Text A" Then
            If Not rng.Rows(i - 1) = "Text B" Then rng.Rows(i - 1).EntireRow.Delete
        End If
        If rng.Cells(i - 1) = "Text B" Then
            x = x + 1
            GoTo Cycle
        End If
    Next
    
End Sub
 

nevsky092315

New Member
Joined
May 3, 2020
Messages
7
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Hi @igold...Thanks for helping here...It works for the last part of TextA to TextB only... doesn't loop around....the loops needs to repeat for 12 iterations at the least (can vary). Please see below table..I want the highlighted data's to remain and all other rows from TextB to TextA get deleted.


././././.
TextA
ABCMarApr
DEFJanDec
TextB
...>'.][.;..........
..//..//..//..//..//..//..//..//..//..//...//.....//..//..//..//..//
''.'.
TextA
GHIMarMar
JKLJanFeb
MNO
PQR
TextB
.../'.'.'
'.][;./';
..//..//..//..//..///..//..//..//..//..//..//..//..//..//..//
TextA
STUJunJul
VWXJanJan
TextB
..//..//..//..//..//..//..//..//..//..//..//..//..//..//..//
 

igold

Well-known Member
Joined
Jul 8, 2014
Messages
2,551
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Edit for errant post.
 

igold

Well-known Member
Joined
Jul 8, 2014
Messages
2,551
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Is this any better. I know you are talking about iterations but I don't think that needs to be a part of the logic.

VBA Code:
Sub RemoveText()

    Dim lRow As Long, i As Long, x As Long
    Dim rng As Range
    
    lRow = Cells(Rows.Count, "A").End(xlUp).Row
    Range("A" & lRow) = "TextA"
Cycle:
    lRow = Cells(Rows.Count, "A").End(xlUp).Row - x
    Set rng = Range("A1:A" & lRow)
    For i = rng.Rows.Count To 2 Step -1
        If rng.Cells(i) = "TextA" Then
            If Not rng.Rows(i - 1) = "TextB" Then rng.Rows(i - 1).EntireRow.Delete
        End If
        If rng.Cells(i - 1) = "TextB" Then
            x = x + 1
            GoTo Cycle
        End If
    Next
    lRow = Cells(Rows.Count, "A").End(xlUp).Row
    For i = lRow To 2 Step -1
        If Cells(i, 1) = "TextB" Then Rows(i).Delete
    Next
    lRow = Cells(Rows.Count, "A").End(xlUp).Row
    Range(Cells(2, 2), Cells(lRow, Columns.Count)).Delete
    Range("A" & lRow).Delete
    
End Sub
 

nevsky092315

New Member
Joined
May 3, 2020
Messages
7
Office Version
  1. 365
Platform
  1. Windows
@igold: Thank you very much!! It did work after removing "Range(Cells(2, 2), Cells(lRow, Columns.Count)).Delete" since I wanted the highlighted cells equivalent column data to remain (sorry I should have mentioned in my description above) Appreciate a lot...Now I need to learn more, understand and practice more to write this type of code.
 

Watch MrExcel Video

Forum statistics

Threads
1,130,313
Messages
5,641,478
Members
417,210
Latest member
rins

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
Top