+ Reply to Thread
Results 1 to 5 of 5

Remove duplicates and change dates - VBA

Hybrid View

wrightyrx7 Remove duplicates and change... 03-03-2014, 08:45 AM
MickG Re: Remove duplicates and... 03-03-2014, 09:51 AM
stanleydgromjr Re: Remove duplicates and... 03-03-2014, 09:56 AM
wrightyrx7 Re: Remove duplicates and... 03-03-2014, 10:01 AM
stanleydgromjr Re: Remove duplicates and... 03-03-2014, 11:50 AM
  1. #1
    Registered User
    Join Date
    03-31-2011
    Location
    Manchester
    MS-Off Ver
    Excel 2010
    Posts
    68

    Remove duplicates and change dates - VBA

    Hi all,

    I need to create a macro that will remove duplicates and leave everyone with one line. But dates will need to be amended before deletion of duplicates.

    The ONE remaining line will have:-

    1. The earliest Start date for each employee.

    2. The greatest end date for each employee UNLESS the line with the greatest start date, has an end date that is BLANK (This means the record is still ongoing, and should remain BLANK).

    Thanks in advance
    Please see image/data for example.

    Capture.JPG

    Example Data.xlsx

  2. #2
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Remove duplicates and change dates - VBA

    Try this:-
    Results start "E1"
    Sub MG03Mar16
    Dim Rng         As Range
    Dim Dn          As Range
    Dim Q
    Set Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
    With CreateObject("scripting.dictionary")
    .CompareMode = vbTextCompare
    For Each Dn In Rng
        If Not .Exists(Dn.Value) Then
            .Add Dn.Value, Array(Dn, Dn.Offset(, 1), Dn.Offset(, 2))
        Else
            Q = .Item(Dn.Value)
                If Dn.Offset(, 1) < Q(1) Then Q(1) = Dn.Offset(, 1)
                If Not Q(2) = "" Then
                    If Dn.Offset(, 2) = "" Then
                        Q(2) = Dn.Offset(, 2)
                    ElseIf Dn.Offset(, 2) > Q(2) Then
                        Q(2) = Dn.Offset(, 2)
                    End If
                End If
            .Item(Dn.Value) = Q
        End If
    Next
    Dim k
    Dim c As Long
    c = 1
    Range("E1:G1").Value = Array("Employees", "Start Date", "End Date")
    
    For Each k In .keys
        c = c + 1
        Cells(c, "E") = .Item(k)(0)
        Cells(c, "F") = .Item(k)(1)
        Cells(c, "G") = .Item(k)(2)
    Next k
    
    End With
    End Sub
    Regards Mick

  3. #3
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Remove duplicates and change dates - VBA

    wrightyrx7,

    With your raw data already sorted/grouped by column A, per your workbook.


    Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

    
    Option Explicit
    Sub RemoveDuplicatesPlus()
    ' stanleydgromjr, 03/03/2014, EF993544
    Dim r As Long, lr As Long, n As Long
    Application.ScreenUpdating = False
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    For r = 2 To lr
      n = Application.CountIf(Columns(1), Cells(r, 1).Value)
      If n > 1 Then
        If Cells(r, 3) = "" Then
          Cells(r, 2) = Cells(r + n - 1, 2)
          Cells(r + 1, 1).Resize(n - 1, 3).ClearContents
        Else
          Cells(r, 2) = Cells(r + n - 1, 2)
          Cells(r + 1, 1).Resize(n - 1, 3).ClearContents
        End If
      End If
      r = r + n - 1
    Next r
    On Error Resume Next
    Range("A2:A" & lr).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    On Error GoTo 0
    Application.ScreenUpdating = True
    End Sub
    Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

    Then run the RemoveDuplicatesPlus macro.
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  4. #4
    Registered User
    Join Date
    03-31-2011
    Location
    Manchester
    MS-Off Ver
    Excel 2010
    Posts
    68

    Re: Remove duplicates and change dates - VBA

    Hi Guys,

    Tested them both and both work perfectly!!!

    Thank you so so much this is a great help!

  5. #5
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Remove duplicates and change dates - VBA

    wrightyrx7,

    Thanks for the feedback.

    You are very welcome. Glad we could help.

    And, come back anytime.

+ 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. Replies: 14
    Last Post: 05-25-2021, 02:13 AM
  2. Replies: 1
    Last Post: 10-23-2012, 09:12 AM
  3. Replies: 5
    Last Post: 02-28-2012, 02:52 PM
  4. Need VBA code to remove entries if there are duplicates (remove them totally)
    By BrandonFromSingapore in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-13-2012, 12:50 AM
  5. Replies: 2
    Last Post: 03-20-2011, 11:19 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