+ Reply to Thread
Results 1 to 6 of 6

delete multiple row based on criteria

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-26-2009
    Location
    Slovakia
    MS-Off Ver
    Excel 2019
    Posts
    244

    delete multiple row based on criteria

    hello,
    I need help with one issue (I uploaded excel worksheet with data) and I need this :
    if in column A in some row is text "today" then delete this row and delete all rows until in row B wont be empty cell

    I have start (If in A is "Today" then ... ) but thas's all

    Sub delete()
    Dim lngRow As Long
        lngRow = 1
        For lngRow = 1 To 24
        If Range("A" & lngRow) = "Today" Then
    
        
        End If
        Next    
    End Sub
    anyone who could help? THANKS !
    Attached Files Attached Files
    Last edited by miso.dca; 12-16-2009 at 05:24 AM.

  2. #2
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: delete multiple row based on criteria

    As long as your pattern is consistent ("Today" with two rows inbetween), this should work. If you actual workbook is different or the patter varies then this will not work fully.

    Option Explicit
    
    Sub delete_rows()
    
        Dim c As Range, lrow As Long
        
        lrow = Cells(Rows.Count, 2).End(xlUp).Row
        
        For Each c In Range("A1:A" & lrow)
            If c.Value = "Today" Then c.EntireRow.Resize(3).Delete
        Next c
    
    End Sub
    Palmetto

    Do you know . . . ?

    You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.

  3. #3
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: delete multiple row based on criteria

    Hi, Try this:-
    Sub MG15Dec11
    Dim Rng As Range, n As Long, R As Range
    Set Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
    For n = Rng.Count To 1 Step -1
        With Range("A" & n)
            If .Value = "Today" Then
                 Set R = Range(.Address & ":" & .Offset(, 1).End(xlDown).Offset(-1).Address)
                  R.EntireRow.Delete
             End If
        End With
    Next n
    End Sub
    Regards Mick

  4. #4
    Forum Contributor
    Join Date
    07-26-2009
    Location
    Slovakia
    MS-Off Ver
    Excel 2019
    Posts
    244

    Re: delete multiple row based on criteria

    Palmetto - tahnks, yes, your solution works but unfortunatelly pattern is different, it isnt always just 2 rows ... (for two rows it isnt very difficult, I'd make it by myself)
    MickG - sorry, but your solution deleted only row where was text "today" I need to delete whole yelllow area, but thanks ! (or do I do anything wrong? )

  5. #5
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: delete multiple row based on criteria

    OK. This works during testing for any pattern. It fills the "inbetween rows" with the word "Today" then just loops through to delete all cells with the word.
    See attached.
    Option Explicit
    
    Sub delete_rows()
    
        Dim c As Range, lrow As Long, i As Long
        
        lrow = Cells(Rows.Count, 2).End(xlUp).Row
        
        Application.ScreenUpdating = False
        
        For Each c In Range("A1:A" & lrow)
            If c.Value = "Today" And c.Offset(1, 0).Value = vbNullString Then
                c.Offset(1, 0).Value = "Today"
            End If
        Next c
        
        For i = lrow To 1 Step -1
            For Each c In Range("A1:A" & lrow)
                    If c.Value = "Today" Then c.EntireRow.Delete
            Next c
        Next i
    
        Application.ScreenUpdating = True
    
    End Sub
    Attached Files Attached Files

  6. #6
    Forum Contributor
    Join Date
    07-26-2009
    Location
    Slovakia
    MS-Off Ver
    Excel 2019
    Posts
    244

    Re: delete multiple row based on criteria

    looks like your solution works perfectly Palmetto but I forgot data at work, so I have to try it tomorrow, now it's quite late and I have to sleep for a while thank you very much ! If everything will be ok, I will close this topic as solved tomorrow thanks again

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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