+ Reply to Thread
Results 1 to 7 of 7

Macro That deletes rows based on date and text

Hybrid View

  1. #1
    Registered User
    Join Date
    06-28-2023
    Location
    ney, york ,usa
    MS-Off Ver
    2013
    Posts
    6

    Macro That deletes rows based on date and text

    I'm slowly trying to automate a weekly report that I put together every Monday (sometimes Tuesday). The example I attached is from Monday 7/3 and hope to be able to use this macro on 7/10 or 7/11.

    I need a macro that will delete the row if the text in Column H starts with "auto" ("auto*" if was used in a formula) AND the date in column C is the previous Thursday thru Current date (6/29,6/30,7/1,7/3,7/4) .

    In this example, 8 rows should end up being deleted. This data is usually 15,000 ish rows long and the dates update every week.

    Thank you so much in advance
    Attached Files Attached Files

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: Macro That deletes rows based on date and text

    Is this not the same as your previous thread?

  3. #3
    Registered User
    Join Date
    06-28-2023
    Location
    ney, york ,usa
    MS-Off Ver
    2013
    Posts
    6

    Re: Macro That deletes rows based on date and text

    No its a little different, previous thread was looking for any text in the cell and the date as criteria for deletion. That was a different part of the report as well

    This Thread I am looking for cell H to start with "auto" and the same date criteria for deletion. I could probably reuse the previous code to search for the date but I have no idea how to look for a specific starting text

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question Re: Macro That deletes rows based on date and text


    Quote Originally Posted by toastmaster12 View Post
    the date in column C is the previous Thursday
    thru Current date (6/29,6/30,7/1,7/3,7/4)
    So you want to not delete 6/28, 6/27, 6/20 and 6/1 ?

  5. #5
    Registered User
    Join Date
    06-28-2023
    Location
    ney, york ,usa
    MS-Off Ver
    2013
    Posts
    6
    Quote Originally Posted by Marc L View Post

    So you want to not delete 6/28, 6/27, 6/20 and 6/1 ?
    For for this example yes, the goal would have the previous Thursday -next Tuesday dates that meet the text criteria deleted. This example is from 7/3 so 6/28 and older should not be deleted

  6. #6
    Registered User
    Join Date
    07-10-2023
    Location
    Chicago, IL
    MS-Off Ver
    O365
    Posts
    4

    Re: Macro That deletes rows based on date and text

    Try this. Coded to be up to 25k rows. It is looking at value in column C. If it is today thru last thursday, AND column H has AUTO in it, it marks it as yes to be deleted and then it runs the next part of the macro and deletes all rows marked as yes.

    Sub Works()
    '
    ' Macro3 Macro
    '
    
    '
    Application.ScreenUpdating = False
    
        Range("O2").Select
        ActiveCell.FormulaR1C1 = _
            "=IF(AND(RC[-12]>=(TODAY()+CHOOSE(WEEKDAY(TODAY()),-3,-4,-5,-6,0,-1,-2)),RC[-12]<=TODAY(),ISNUMBER(FIND(""AUTO"",RC[-7]))),""yes"",""no"")"
        Range("O2").Select
            Selection.AutoFill Destination:=Range("O2:O" & Range("A" & Rows.Count).End(xlUp).Row)
    
        Columns("O:O").Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False
        Call Delete
            Columns("O:O").Select
        Selection.Delete Shift:=xlToLeft
        Application.ScreenUpdating = True
    End Sub
    Sub Delete()
    Dim LastRow As Long
    Dim i As Long
    
    LastRow = Range("O25000").End(xlUp).Row
    For i = LastRow To 1 Step -1
    If Range("O" & i) = "yes" Then
    Range("O" & i).EntireRow.Delete
    End If
    Next
    End Sub
    Last edited by vidguru1979; 07-10-2023 at 05:28 PM.

  7. #7
    Registered User
    Join Date
    07-10-2023
    Location
    Chicago, IL
    MS-Off Ver
    O365
    Posts
    4

    Re: Macro That deletes rows based on date and text

    This should be dynamic now so if your data goes past 25k rows.

     
    Sub Works()
    '
    ' Macro3 Macro
    '
    
    '
    Application.ScreenUpdating = False
    
        Range("O2").Select
        ActiveCell.FormulaR1C1 = _
            "=IF(AND(RC[-12]>=(TODAY()+CHOOSE(WEEKDAY(TODAY()),-3,-4,-5,-6,0,-1,-2)),RC[-12]<=TODAY(),ISNUMBER(FIND(""AUTO"",RC[-7]))),""yes"",""no"")"
        Range("O2").Select
            Selection.AutoFill Destination:=Range("O2:O" & Range("A" & Rows.Count).End(xlUp).Row)
    
        Columns("O:O").Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False
        Call Delete
            Columns("O:O").Select
        Selection.Delete Shift:=xlToLeft
         Application.Goto Range("A1"), Scroll:=True
            Range("A1").Select
        Application.ScreenUpdating = True
    End Sub
    
    
    Sub Delete()
        Dim LastRow As Long
        Dim i As Long
        Dim rng As Range
        Dim deleteRange As Range
    
        ' Find the last cell with a value in column O
        Set rng = Range("O1", Range("O" & Rows.Count).End(xlUp))
        LastRow = rng.Row + rng.Rows.Count - 1
    
        ' Loop through the rows in reverse order
        For i = LastRow To 1 Step -1
            If Range("O" & i) = "yes" Then
                If deleteRange Is Nothing Then
                    Set deleteRange = Range("O" & i)
                Else
                    Set deleteRange = Union(deleteRange, Range("O" & i))
                End If
            End If
        Next i
    
        ' Delete the rows
        If Not deleteRange Is Nothing Then
            deleteRange.EntireRow.Delete
        End If
    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. [SOLVED] Help Edit Loop that deletes Rows based on Date condition.
    By swade730 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-08-2015, 02:16 PM
  2. [SOLVED] Need Help cleaning up Macro that imports text file and deletes unnecessary rows
    By ppilot in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-16-2013, 10:44 PM
  3. Macro Deletes all rows except....
    By TheDriver85 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-25-2012, 06:37 PM
  4. Macro that deletes rows below a certain value
    By Biased Historian in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 01-15-2011, 09:45 PM
  5. Macro that Deletes All rows except first row
    By curtney in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-18-2006, 01:22 PM
  6. Macro that deletes certain rows only
    By supamari0 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-09-2006, 11:15 AM
  7. Macro that deletes certain rows and not others
    By Roger in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-02-2005, 07:06 PM

Tags for this Thread

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