VBA to find most recent Invoice Date for each Customer

Mikilive

New Member
Joined
May 17, 2006
Messages
6
I'm working on a macro that eventually takes a 20,000 row worksheet down to under 5,000. One of the first issues I cannot figure out, or find an answer to, is how to remove all but the most recent invoice for each customer. Some of the customers could have 1 or 2 invoices where most have 4 per month. I've looked for Max Date Filters where I can filter out the max date per customer and remove the remaining rows but since I'm dealing with multiple rows of data for each customer I can't find a solution that works. Below is just a snip of the spreadsheet but for this example I would like to end up with only the rows dated 11/21/23 for customer 10093631 and rows dated 11/20/2023 for customer 10108607. There will be additional filtering, look ups and formulas written into the macro to replace a lot of manual work that is currently being done to get the data into shape so starting the macro with a solution to this issue would be preferable rather then pivots or formulas in excel.

Max Invoice Date.JPG


Any help would be greatly appreciated.
Thank you
 
I tried your code and got a Type Mismatch on this section j = a(i + 1, 6)
Which tends to suggest the dates might be text that look like dates. Although you already have an accepted solution, have a try with the following amended code, which turned the sample used in post #6 into this:
Mikilive.xlsm
ABCDEFGH
1CustomerAddressCityStateService TicketBilling DateMaterial NumberValue
2100936313061 INTERSTATE PKWYBRUNSWICKOH417474455011/21/2023X2
3100936313062 INTERSTATE PKWYBRUNSWICKOH417474455011/21/2023X1132
4100936313063 INTERSTATE PKWYBRUNSWICKOH417474455011/21/2023X106
510108607834 BESSEMER STMEADVILLEPA417433698311/20/2023X2
610108607835 BESSEMER STMEADVILLEPA417433698311/20/2023X1132
710108607836 BESSEMER STMEADVILLEPA417433698311/20/2023X106
8
Sheet1


Amended code:
VBA Code:
Option Explicit
Sub Mikilive_V2()
    Application.ScreenUpdating = False
    Dim ws As Worksheet, LRow As Long, LCol As Long
    Set ws = Worksheets("Sheet1")                   '<-- *** Change to actual sheet name ***
    LRow = ws.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
    LCol = ws.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column + 1
   
    With ws
        .Columns("A:H").Sort Key1:=.Range("A1"), order1:=xlAscending, _
        Key2:=.Range("F1"), order2:=xlAscending, Header:=xlYes
    End With
   
    Dim a, b, i As Long, s As String
    a = ws.Range("A2:H" & LRow)
    ReDim b(1 To UBound(a, 1), 1 To 1)
    For i = UBound(a, 1) - 1 To 1 Step -1
        If s = "" Then s = a(i + 1, 6)
        If a(i, 1) <> a(i + 1, 1) Then s = a(i, 6)
        If a(i, 1) = a(i + 1, 1) And a(i, 6) <> s Then b(i, 1) = 1
    Next i
   
    ws.Cells(2, LCol).Resize(UBound(b, 1)).Value = b
    i = WorksheetFunction.Sum(ws.Columns(LCol))
    If i > 0 Then
        ws.Range(ws.Cells(2, 1), ws.Cells(LRow, LCol)).Sort Key1:=ws.Cells(2, LCol), _
        order1:=xlAscending, Header:=xlNo
        ws.Cells(2, LCol).Resize(i).EntireRow.Delete
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

Forum statistics

Threads
1,215,123
Messages
6,123,182
Members
449,090
Latest member
bes000

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top