+ Reply to Thread
Results 1 to 7 of 7

Thread: remove duplicate records based on most recent date

  1. #1
    Registered User
    Join Date
    01-11-2012
    Location
    West Palm Beach, Florida
    MS-Off Ver
    Excel 2007
    Posts
    4

    remove duplicate records based on most recent date

    I have a set of inventory records (84,000 user workstations) that have some duplicate records (~2%). The database is suppose to have a single record per workstation (unique ID). Each record has a date associated with it, and the duplicate records each have a different date. I need to keep the most recent record (based on date) and discard the duplicate records with the old dates.
    Attached Files Attached Files
    Last edited by dajlwj; 01-12-2012 at 08:29 AM. Reason: Attached a sample file

  2. #2
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,371

    Re: remove duplicate records based on most recent date

    Please attach a sample workbook so we can help you faster.
    Cheers,
    Arlette

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    01-11-2012
    Location
    West Palm Beach, Florida
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: remove duplicate records based on most recent date

    Thanks Arlette, I've attached an example file to my original post. For clarity the duplicated items are hilighted in red. I need only the most recent (based on date) which means only one of the either 2 or 3 entries having the same device name.

    -Dave

  4. #4
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,371

    Re: remove duplicate records based on most recent date

    Use this code -
    Option Explicit
    Dim lrow As Long
    Dim i As Long
    
    Sub sort_latest()
    
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A:A") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("C:C") _
        , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A:D")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    With Worksheets("Sheet1")
        lrow = .Range("A" & Rows.Count).End(xlUp).Row
        For i = lrow To 2 Step -1
            If .Range("A" & i).Value = .Range("A" & i - 1).Value Then
                If .Range("C" & i).Value < .Range("C" & i - 1).Value Then
                    .Rows(i & ":" & i).Delete
                    lrow = lrow - 1
                End If
            End If
        Next i
    End With
    
    End Sub
    Cheers,
    Arlette

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  5. #5
    Registered User
    Join Date
    01-11-2012
    Location
    West Palm Beach, Florida
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: remove duplicate records based on most recent date

    Arlette,
    I'm afraid I don't know how to implement this solution. I was hoping for a solution that used cascaded or nested excel functions that I could enter into a cell, then perhaps select only those records flagged having the most recent date.
    Thanks,
    -Dave

  6. #6
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,371

    Re: remove duplicate records based on most recent date

    I am not very fluent in complicated formulae, but can provide a solution with VBA.

    All you have to do is press Alt+F11, you will get a code window. On the left hand side, you will see Microsoft excel objects. Right click and select insert -> Module. Copy the above code into that module. Go back to your excel sheet, select View -> Macros and select the macro to be run.
    Cheers,
    Arlette

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  7. #7
    Registered User
    Join Date
    01-11-2012
    Location
    West Palm Beach, Florida
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: remove duplicate records based on most recent date

    I've have a solution that works for me. I'd be interested in a critique if you have the time. If you open the test file in my previous post, the columns are in the following order - Device, OS, Date, Type. In my solution I moved the Date over one column to put it next to the Device. This made the array for the VLOOKUP compact. The one other thing that was needed was to sort the list by Date from newest to oldest. This guarenteed that the first Date that existed for a given Device was the most recent. I next inserted a column (NewestDate) with the formula =IF((VLOOKUP(A2,A:B,2,FALSE)=B2), B2, "old") to identify old dates. The Dups column is left in for testing.

    My interest was to discover an Excel function solution to do this. I appreciate the code solution previously posted, and I also know this is possible to accomplish manually using spread sheet menu options.

    -Dave
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0