+ Reply to Thread
Results 1 to 4 of 4

Delete Entire Row on Every Sheet if Date Condition Matches

Hybrid View

  1. #1
    Registered User
    Join Date
    12-28-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    4

    Delete Entire Row on Every Sheet if Date Condition Matches

    What I am trying to do is filter 2nd column(B) for every row for every sheet and delete if the condition matches; ">11/12/2019" in case of the following code.

    The code would run into 1004 on the first sheet where the condition was matched.


    
    Option Explicit
    Sub TooTiredToNameThis()
    
    Dim MySheet As Worksheet, MyRange As Range
    Dim LastRow As Long, LastCol As Long
    Dim ActSheet As Integer
    
    Application.DisplayAlerts = False
    
    For ActSheet = 1 To Sheets.Count
    
    Set MySheet = ThisWorkbook.Worksheets(ActSheet)
    
    With MySheet
        LastRow = .Range("B" & .Rows.Count).End(xlUp).Row
        'LastCol = .Range("B" & .Columns.Count).End(xlToLeft).Column
        Set MyRange = .Range(.Cells(1, 2), .Cells(LastRow, 2)) '2 was LastCol
    End With
    
    
    With MyRange
        .AutoFilter Field:=1, Criteria1:=">12/11/2019"
        .SpecialCells(xlCellTypeVisible).Resize(.Rows.Count).Rows.Delete
    End With
    
    With MySheet
        .AutoFilterMode = False
        If .FilterMode = True Then
            .ShowAllData
        End If
    End With
    
    Next
    
    Application.DisplayAlerts = True
    
    End Sub
    Sample Data:

    Blah Date AnotherBlah BlahAgain Woof WoofAgain WoofWoof LastOne
    Blah1 10/12/2019 AnotherBlah1 BlahAgain1 Woof1 WoofAgain1 WoofWoof1 LastOne1
    Blah2 09/12/2019 AnotherBlah2 BlahAgain2 Woof2 WoofAgain2 WoofWoof2 LastOne2
    Blah3 13/12/2019 AnotherBlah3 BlahAgain3 Woof3 WoofAgain3 WoofWoof3 LastOne3


    Can you please help me spot what went wrong and why?
    Last edited by c052463; 01-16-2020 at 11:52 AM.

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,032

    Re: Delete Entire Row on Every Sheet if Date Condition Matches

    Try:
    Sub DeleteRows()
        Application.ScreenUpdating = False
        With ActiveSheet.Cells(1).CurrentRegion
            .AutoFilter 2, ">12/11/2019"
        End With
        With ActiveSheet
            .AutoFilter.Range.Offset(1).EntireRow.Delete
            If .AutoFilterMode = True Then .AutoFilterMode = False
        End With
        Application.ScreenUpdating = True
    End Sub
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Registered User
    Join Date
    12-28-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Delete Entire Row on Every Sheet if Date Condition Matches

    Thanks man my head is mush right now and you helped me get through this.

    Here's the final code I did to do it to all sheets.

    Sub ThanksMumps1()
    
    Application.ScreenUpdating = False
    
    Dim ActSheet As Integer
    
    For ActSheet = 1 To Sheets.Count
    
    With ActiveSheet.Cells(1).CurrentRegion
        .AutoFilter 2, ">12/12/2019"
    End With
    
    With ActiveSheet
        .AutoFilter.Range.Offset(1).EntireRow.Delete
        If .AutoFilterMode = True Then .AutoFilterMode = False
    End With
    
    Next
    
    Application.ScreenUpdating = True
    
    End Sub

  4. #4
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,032

    Re: Delete Entire Row on Every Sheet if Date Condition Matches

    Are you sure that it worked properly? I would think the macro should be something like this:
    Sub ThanksMumps1()
        Application.ScreenUpdating = False
        Dim ActSheet As Integer
        For ActSheet = 1 To Sheets.Count
            With Sheets(ActSheet).Cells(1).CurrentRegion
                .AutoFilter 2, ">12/12/2019"
            End With
            With Sheets(ActSheet)
                .AutoFilter.Range.Offset(1).EntireRow.Delete
                If .AutoFilterMode = True Then .AutoFilterMode = False
            End With
        Next ActSheet
        Application.ScreenUpdating = True
    End Sub

+ 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. Delete entire row based on certain condition
    By roberco30 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-04-2017, 10:41 PM
  2. delete empty entire row with condition
    By ash3angel in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 06-29-2015, 08:29 AM
  3. [SOLVED] VBA to delete entire row based on condition met
    By shiva_reshs in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-17-2014, 05:48 PM
  4. Macro to removed entire row on Sheet A if a text matches a condition on Sheet B
    By andy@2ggroup.ca in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-03-2012, 04:22 PM
  5. [SOLVED] Macro to delete entire row on a condition
    By kgonzalbo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-30-2011, 12:39 AM
  6. Delete entire row on condition
    By bouitac in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-06-2009, 04:58 PM
  7. [SOLVED] Can I delete an entire row if condition is not met?
    By Christine in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 05-04-2006, 04:50 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