Macro request to delete all rows between two specific text values whose rows are dynamic

crab transplant

New Member
Joined
May 21, 2014
Messages
2
Hello excel guru's,

I have a data output that has two text values which repeat in rows throughout the dataset, Text A and Text B.

The number of rows between Text A and Text B are dynamic, but Text A and Text B are always the bookends and always reside in Column C. What I am looking for is to delete the rows between Text A and Text B throughout the worksheet using a Macro. Additionally there are other rows, not between Text A and Text B that should not be deleted.

Additionally I would explain the problem as: For every instance of Text A, delete rows until instance of Text B. Then look for next instance of Text A and repeat.

I have tried, but failed to adopt these posts and macros http://www.mrexcel.com/forum/excel-...-between-two-other-rows-certain-criteria.html and http://www.mrexcel.com/forum/excel-questions/6483-delete-rows-between-two-specified-values.html as I am a Marco Noob.

Thank you in advance for your assistance.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I BELIEVE this is what you are looking for. Since macros cannot be undone, I strongly suggest you save your workbook before running the macro though.
It should run pretty quick too since there is only a single DELETE statement.

Macro runs on the currently active sheet and you can change your bookend values at the very beginning of the code by overwriting the "Text A", "Text B" string values.

Code:
Sub DeletebyBookends()
Dim strStart As String, strEnd As String
Dim DELETEMODE As Boolean
Dim DelRng As Range
    strStart = "Text A"
    strEnd = "Text B"
    
    DELETEMODE = False
    For r = 1 To Range("C" & Rows.Count).End(xlUp).Row  'first to last used row
        
        If Range("C" & r).Value = strEnd Then DELETEMODE = False
        
        If DELETEMODE Then
            'Create a Delete Range that will be used at the end
            If DelRng Is Nothing Then
                Set DelRng = Range("A" & r)
            Else
                Set DelRng = Application.Union(DelRng, Range("A" & r))
            End If
        End If
        
        If Range("C" & r).Value = strStart Then DELETEMODE = True
    Next r
    
    'Delete the Range compiled from above
    If Not DelRng Is Nothing Then DelRng.EntireRow.Delete xlShiftUp
    
End Sub
 
Upvote 0
Hi, Thanks for your nice macro code. My problem is a lot similar to your solution just with one modification. I want to delete all rows INCLUDING the rows with strings "Text A" and "Text B".

Thanks!

 
Upvote 0
You can use this to delete rows between two strings

Code:
On Error Resume Next
Range(Range("C:C").Find("Text A"), Range("C:C").Find("Text B")).EntireRow.Delete
On Error GoTo 0

or just clear it's contents:

Code:
On Error Resume Next
Range(Range("C:C").Find("Text A"), Range("C:C").Find("Text B")).Clearcontents
On Error GoTo 0
 
Last edited:
Upvote 0
Hi, Thanks for your nice macro code. My problem is a lot similar to your solution just with one modification. I want to delete all rows INCLUDING the rows with strings "Text A" and "Text B".

Thanks!


Relatively straightforward change to my previous code accomplishes this. Simply swap the two lines of when DELETEMODE gets turned on/off.

Rich (BB code):
Sub DeletebyBookends_INCLUSIVE()
Dim strStart As String, strEnd As String
Dim DELETEMODE As Boolean
Dim DelRng As Range
    strStart = "Text A"
    strEnd = "Text B"
    
    DELETEMODE = False
    For r = 1 To Range("C" & Rows.Count).End(xlUp).Row  'first to last used row
        
        If Range("C" & r).Value = strStart Then DELETEMODE = True
        
        If DELETEMODE Then
            'Create a Delete Range that will be used at the end
            If DelRng Is Nothing Then
                Set DelRng = Range("A" & r)
            Else
                Set DelRng = Application.Union(DelRng, Range("A" & r))
            End If
        End If
        
        If Range("C" & r).Value = strEnd Then DELETEMODE = False
    Next r
    
    'Delete the Range compiled from above
    If Not DelRng Is Nothing Then DelRng.EntireRow.Delete xlShiftUp
    
End Sub



Tocy777, your code is also good and a clever single line approach. The only thing you would need to add would be looping as the original request was for multiple instances of the TextA/TextB in the sheet. The only potential problem I imagine with yours would be if there was a stray TextB PRIOR to the first TextA. In that instance, your code would delete all of the rows that were supposed to be kept and leave all of the rows that are supposed to be deleted.
 
Last edited:
Upvote 0
Hi BiocideJ
The modified code is not working the way I want. What I require is that for every instance of "Text A", delete rows until instance of "Text B" (Rows that contain Text A and Text B are also need to be deleted). Then look for next instance of Text A until Text B and repeat in the similar way. Your first code did fine except it included Text A and Text b and deleted all rows in-between. I just want that all Rows starting from Text A until Text B are deleted every-time.

My data is also similar to the one posted in the question initially.
 
Upvote 0
Hi BiocideJ, sorry for the last reply. I was doing a small mistake at my end. I didnt change the strEnd option. Your code works fine.
Thanks for your quick help!
 
Upvote 0
Also, from a PM the following two versions will keep either the FIRST value (TEXT A)
Code:
Sub DeletebyBookends_KEEP_FIRST()
Dim strStart As String, strEnd As String
Dim DELETEMODE As Boolean
Dim DelRng As Range
    strStart = "Text A"
    strEnd = "Text B"
    
    DELETEMODE = False
    For r = 1 To Range("C" & Rows.Count).End(xlUp).Row  'first to last used row
        
        If DELETEMODE Then
            'Create a Delete Range that will be used at the end
            If DelRng Is Nothing Then
                Set DelRng = Range("A" & r)
            Else
                Set DelRng = Application.Union(DelRng, Range("A" & r))
            End If
        End If
                
        If Range("C" & r).Value = strStart Then DELETEMODE = True
        If Range("C" & r).Value = strEnd Then DELETEMODE = False
        
    Next r
    
    'Delete the Range compiled from above
    If Not DelRng Is Nothing Then DelRng.EntireRow.Delete xlShiftUp
    
End Sub

Or the LAST value (TEXT B)
Code:
Sub DeletebyBookends_KEEP_LAST()
Dim strStart As String, strEnd As String
Dim DELETEMODE As Boolean
Dim DelRng As Range
    strStart = "Text A"
    strEnd = "Text B"
    
    DELETEMODE = False
    For r = 1 To Range("C" & Rows.Count).End(xlUp).Row  'first to last used row
        
        If Range("C" & r).Value = strStart Then DELETEMODE = True
        If Range("C" & r).Value = strEnd Then DELETEMODE = False
        
        If DELETEMODE Then
            'Create a Delete Range that will be used at the end
            If DelRng Is Nothing Then
                Set DelRng = Range("A" & r)
            Else
                Set DelRng = Application.Union(DelRng, Range("A" & r))
            End If
        End If
        
    Next r
    
    'Delete the Range compiled from above
    If Not DelRng Is Nothing Then DelRng.EntireRow.Delete xlShiftUp
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,520
Messages
6,179,266
Members
452,902
Latest member
Knuddeluff

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