Consulting

Results 1 to 9 of 9

Thread: Macro to delete all rows between two different text stings in column A

  1. #1
    VBAX Tutor
    Joined
    Jul 2015
    Posts
    212
    Location

    Macro to delete all rows between two different text stings in column A

    As a means to an end, the solution to this post might help remove another problem I have outlined in another post? In column A, I am after a macro to delete all rows (blank or otherwise between two specified strings of text. Namely "TAB:" and "SKY". They will only appear once each in a column but spaced indeterminably:
    jon
    Param

    TAB:
    frit

    brat
    Gun

    SKY
    sop
    net

    The sheet after the macro, would appear as:
    jon
    Param

    TAB:
    SKY
    sop
    net

    Thanks

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Option Explicit
    Sub test()
        Dim iTABrow As Long, iSKYrow As Long
        
        iTABrow = 0
        iSKYrow = 0
        
        On Error Resume Next
        iTABrow = Application.WorksheetFunction.Match("TAB:", ActiveSheet.Columns(1), 0) + 1
        iSKYrow = Application.WorksheetFunction.Match("SKY", ActiveSheet.Columns(1), 0) - 1
        On Error GoTo 0
        
        
        If iTABrow = 1 Or iSKYrow = -1 Then Exit Sub
        
        ActiveSheet.Rows(iTABrow & ":" & iSKYrow).Delete
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Tutor
    Joined
    Jul 2015
    Posts
    212
    Location
    Just as cream rises to the top, and greatness to the fore, you have once again proved your skills, Paul. Thankyou, case closed.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    <blush>

    You can use [Thread Tools] on the menu above your #1 to make it [Solved]
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Tutor
    Joined
    Jul 2015
    Posts
    212
    Location
    Can I add an addendum to this post? Paul, a weird issue has arisen? Code works well if you type the text into a spreadsheet and enact the macro.... BUT....if the text is pasted from a web page query into excel, you get an error with the line, "ActiveSheet.Rows(iTABrow..........Delete, which is what happens in reality. Any solutions?
    Cheers

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Attach something to look at

    Paste the text and save the WB as is
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    VBAX Tutor
    Joined
    Jul 2015
    Posts
    212
    Location
    Here's typical worksheet after the web page has been pasted in by a web query. In this instance I want to delete all rows between the occurrence of text "TAB:", 'A98" and "Print This Meeting", 'A266'. (Cell references always differ).

    I think many of my problems occur, in that I make the example too simplified to the real world case?
    Attached Files Attached Files

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    1. In that file in A98 there is TAB: followed by 4 spaces

    2. In column A there are 24 cells containing 'Sky', 15 with 'Sky' in the first position

    3. 'Print This Meeting' or 'SKY' ??? The macro below search for 'Print This Meeting'

    Option Explicit
    
    Sub test()
        Dim iTABrow As Long, iMTGrow As Long
         
        iTABrow = 0
        iMTGrow = 0
        
        On Error Resume Next
    
        iTABrow = ActiveSheet.Columns(1).Find(What:="TAB:", After:=ActiveSheet.Cells(1, 1), _
            LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False).Row + 1
    
        iMTGrow = ActiveSheet.Columns(1).Find(What:="Print This Meeting", After:=ActiveSheet.Cells(1, 1), _
            LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False).Row - 1
    
        On Error GoTo 0
         
        If iTABrow = 1 Or iMTGrow = -1 Then Exit Sub
         
        ActiveSheet.Rows(iTABrow & ":" & iMTGrow).Delete
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  9. #9
    VBAX Tutor
    Joined
    Jul 2015
    Posts
    212
    Location
    "Exhalted", the best word to describe someone who surpasses past achievements, that being you Sir!

Posting Permissions

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