+ Reply to Thread
Results 1 to 8 of 8

Deleting all rows between two strings

Hybrid View

  1. #1
    Registered User
    Join Date
    05-06-2010
    Location
    Derby
    MS-Off Ver
    Excel 2007
    Posts
    9

    Deleting all rows between two strings

    Hello,

    I’d be grateful for your help making a macro to delete all rows between two specified strings.

    It would look for two strings, always in column A: "Item Title" and any string beginning "Library" or "LIBRARY".

    It would delete all the rows between these cells (including the rows in which these two strings are found).

    Notes:
    - "Item Title" and "Library*" may appear multiple times in the worksheet, so multiple ranges may require deletion.
    - The number of rows between these two strings is unpredictable.
    - There may be up to 1,000 rows to check through.

    Example Workbook:
    The attachment shows a typical initial state. For clarity, I’ve highlighted in red two ranges that I’d like the macro to remove (they wouldn’t be highlighted in "real life"!).

    range_deletion_question.xlsx

    Thank you for any help you can offer.
    Matt

  2. #2
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Deleting all rows between two strings

    Sub mfd()
    Dim FirstRow, LastRow, EndRow
    Dim IT As Boolean, LIB As Boolean
    IT = False
    LIB = False
    
    EndRow = Cells(Rows.Count, "A").End(xlUp).Row
    
    For i = EndRow To 1 Step -1
        If Cells(i, 1).Value = "Item Title" Then
            IT = True
            FirstRow = i
        ElseIf UCase(Cells(i, 1).Value) = "LIBRARY" Then
            LIB = True
            LastRow = i
        End If
        
        If IT = True And LIB = True Then
        Rows(FirstRow & ":" & LastRow).EntireRow.Delete
        IT = False
        LIB = False
        FirstRow = ""
        LastRow = ""
        End If
    Next
    End Sub
    Last edited by walruseggman; 12-03-2014 at 11:06 AM.

  3. #3
    Registered User
    Join Date
    05-06-2010
    Location
    Derby
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Deleting all rows between two strings

    Hello Walruseggman. Liking the Beatles reference.

    Thank you very much for offering a solution; unfortunately nothing seems to happen when I run it - not even an error.

    I've attached the workbook, now with the macro. You'll probably find it works fine and I'm probably being a moron...

    Attachment 362617

    Best wishes,

    Matt

  4. #4
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Deleting all rows between two strings

    Yeah, I probably should have looked at your example. I wasn't aware that the Library cells would have more text in them than just the word Library.

    Anyway, I've tweeked the code and it does delete all your red text cells in your original sheet (the link to the new one you posted doesn't seem to work.)

    Let me know how it goes.

    Sub mfd()
    Dim FirstRow, LastRow, EndRow
    
    EndRow = Cells(Rows.Count, "A").End(xlUp).Row
    
    For i = EndRow To 1 Step -1
        
        If UCase(Cells(i, 1).Value) Like "LIBRARY*" Then
            LastRow = i
            FirstRow = ""
        ElseIf Cells(i, 1).Value = "Item Title" And LastRow <> "" Then
            FirstRow = i
        End If
        
        If LastRow <> "" And FirstRow <> "" Then
        Rows(FirstRow & ":" & LastRow).EntireRow.Delete
        FirstRow = ""
        LastRow = ""
        End If
    Next
    End Sub

  5. #5
    Registered User
    Join Date
    05-06-2010
    Location
    Derby
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Deleting all rows between two strings

    You're an absolute legend. Thank you very much, that works great!

  6. #6
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Deleting all rows between two strings

    Cool. Reputation would be appreciated (the * in the lower left of posts), and dont forget to mark this thread as SOLVED

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Deleting all rows between two strings

    Maybe:

    Sub mfd()
    Dim w As Range
    Dim x As Long
    Dim z As Long
    Dim y As Long
    y = Range("A" & Rows.Count).End(3)(2).Row
    Cells(y, 1).Select
    Do Until ActiveCell.Row < 14
    Set w = Range(Cells(2, 1), Cells(y, 1)).Find("*Library*", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
        If Not w Is Nothing Then
            x = w.Row
        End If
    Set w = Nothing
    Cells(x, 1).Select
    Do Until ActiveCell.Offset(-1) = "Item Title" Or ActiveCell.Row = 3
        ActiveCell.Offset(-1).Select
    Loop
    z = ActiveCell.Row - 1
    Range(Cells(z, 1), Cells(x, 1)).EntireRow.Delete
    y = ActiveCell.Row - 2
    Cells(y, 1).Select
    Loop
    End Sub

  8. #8
    Registered User
    Join Date
    05-06-2010
    Location
    Derby
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Deleting all rows between two strings

    Hi John - that worked perfectly too. Thank you very much indeed for your time.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Deleting strings from formulas
    By twt4757 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-11-2013, 02:07 PM
  2. Replies: 2
    Last Post: 08-20-2013, 07:52 AM
  3. [SOLVED] Deleting part of a column without deleting whole rows.
    By dstrdOne in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-30-2013, 11:42 AM
  4. Macro help deleting rows with selected character strings
    By rliccion in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-27-2009, 01:45 PM
  5. deleting rows containing certain strings
    By pilch in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-14-2008, 05:23 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1