+ Reply to Thread
Results 1 to 13 of 13

Delete Rows Based on Dates, etc.

Hybrid View

  1. #1
    Registered User
    Join Date
    07-13-2005
    Location
    Louisville ky
    MS-Off Ver
    2018
    Posts
    90

    Delete Rows Based on Dates, etc.

    I posted this over on Woody's, but haven't heard anything, and am under the gun to get an answer.
    Please see attached. What I would like is a macro that would delete all the rows that have zero receipts for the total month of April 2011 (for example all of the rows for the month of April for the points Jeff and Sam would be deleted). On the other hand, if on any day during that period, there were receipts, none of rows for that point for the month of April 2011 would be deleted (for example John, Mary and Bill). Any ideas? Thanks in advance.
    Attached Files Attached Files

  2. #2
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Delete Rows Based on Dates, etc.

    Try as a test...

    Sub FilterDelete()
        With Range("C1", Range("C" & Rows.Count).End(xlUp))
            .AutoFilter field:=1, Criteria1:="-"
            .Offset(1).EntireRow.Delete
            .AutoFilter
        End With
    End Sub
    HTH
    Regards, Jeff

  3. #3
    Registered User
    Join Date
    07-13-2005
    Location
    Louisville ky
    MS-Off Ver
    2018
    Posts
    90

    Re: Delete Rows Based on Dates, etc.

    Thanks for your quick response. This almost works, except that it is deleting those days of the month where zero receipts are recorded for those points that otherwise have receipts for the other days of the month. For example, in addition to all of Jeff and all of Sam being deleted, it is also deleting 4/2-4/4 of John, as well as other days of not only John but of Mary and Bill as well.

  4. #4
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Delete Rows Based on Dates, etc.

    Okay if I understand correctly, if Jeff is completely blank for the entire month you want it deleted, but if John has values for the month yet some days and blank you still want those intact?

  5. #5
    Registered User
    Join Date
    07-13-2005
    Location
    Louisville ky
    MS-Off Ver
    2018
    Posts
    90

    Re: Delete Rows Based on Dates, etc.

    Yes, that is correct.

  6. #6
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Delete Rows Based on Dates, etc.

    Sorry I missed that part the first time.

    Give this a test. It utilizes Sheet3 to get a unique count of names and whether or not the total volumn is 0. If 0 then that person is deleted from Sheet1 and if not 0 then kept.

    Sub Macro1()
        Dim wsSrc As Worksheet: Set wsSrc = Sheets("Sheet1")
        Dim wsDest As Worksheet: Set wsDest = Sheets("Sheet3")
        Dim i As Long
        Dim LR As Long
        With wsSrc
            .Columns("A:A").AdvancedFilter Action:=xlFilterCopy, _
                CopyToRange:=wsDest.Range("A1"), Unique:=True
        End With
        With wsDest
            .Range("B2:B" & .Range("A" & .Rows.Count).End(xlUp).Row).FormulaR1C1 _
                = "=SUMIF(Sheet1!R2C1:R151C1,RC[-1],Sheet1!R2C3:R151C3)"
            LR = .Range("A" & .Rows.Count).End(xlUp).Row
            For i = 2 To LR
                If .Range("B" & i).Value = 0 Then
                    With wsSrc
                        With .Range("A1", .Range("A" & .Rows.Count).End(xlUp))
                            .AutoFilter field:=1, Criteria1:=wsDest.Range("A" & i).Value
                            .Offset(1).EntireRow.Delete
                            .AutoFilter
                        End With
                    End With
                End If
            Next i
        End With
    End Sub

  7. #7
    Registered User
    Join Date
    07-13-2005
    Location
    Louisville ky
    MS-Off Ver
    2018
    Posts
    90

    Re: Delete Rows Based on Dates, etc.

    Thanks-works great! Sorry for the confusion.

  8. #8
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Delete Rows Based on Dates, etc.

    No worries the confusion was on my part...I often read too fast.

    If you are satisfied with the answer provided, please don’t forget to mark the thread as solved.

    How to mark a thread Solved
    Go to the first post
    Select
    -- Edit
    -- Go Advanced
    -- Below the word Title you will see a dropdown with the word No prefix
    -- Update to Solved and then select Save

  9. #9
    Registered User
    Join Date
    07-13-2005
    Location
    Louisville ky
    MS-Off Ver
    2018
    Posts
    90

    Re: Delete Rows Based on Dates, etc.

    Jeff,
    Oops! May have spoken too soon. I tried running it on the revised table, and got a runtime error. See what you come up with.
    Thanks again.
    Attached Files Attached Files

  10. #10
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Delete Rows Based on Dates, etc.

    I'm sorry that is my fault. I forgot to build in the LR variable for the Sumif.

    I'll post a revision in a few

  11. #11
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Delete Rows Based on Dates, etc.

    Try...

    Sub Macro1()
        Dim wsSrc As Worksheet: Set wsSrc = Sheets("Sheet1")
        Dim wsDest As Worksheet: Set wsDest = Sheets("Sheet3")
        Dim i As Long
        Dim LR As Long
        Dim LR1 As Long
        Application.ScreenUpdating = False
        With wsSrc
            .Columns("A:A").AdvancedFilter Action:=xlFilterCopy, _
                CopyToRange:=wsDest.Range("A1"), Unique:=True
        End With
        With wsDest
            LR1 = wsSrc.Range("A" & .Rows.Count).End(xlUp).Row
            .Range("B2:B" & .Range("A" & .Rows.Count).End(xlUp).Row).FormulaR1C1 _
                = "=SUMIF(Sheet1!R2C1:R" & LR1 & "C1,RC[-1],Sheet1!R2C3:R" & LR1 & "C3)"
            LR = .Range("A" & .Rows.Count).End(xlUp).Row
            For i = 2 To LR
                If .Range("B" & i).Value = 0 Then
                    With wsSrc
                        With .Range("A1", .Range("A" & .Rows.Count).End(xlUp))
                            .AutoFilter field:=1, Criteria1:=wsDest.Range("A" & i).Value
                            .Offset(1).EntireRow.Delete
                            .AutoFilter
                        End With
                    End With
                End If
            Next i
        End With
        Application.ScreenUpdating = True
    End Sub

  12. #12
    Registered User
    Join Date
    07-13-2005
    Location
    Louisville ky
    MS-Off Ver
    2018
    Posts
    90

    Re: Delete Rows Based on Dates, etc.

    Thanks again...

  13. #13
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Delete Rows Based on Dates, etc.

    You're welcome

    Please don't forget to mark your thread as solved

+ 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