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

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
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.
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
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
..//..//..//..//..//..//..//..//..//..//..//..//..//..//..//
 
Upvote 0
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
 
Upvote 0
@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.
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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