+ Reply to Thread
Results 1 to 15 of 15

Removing Duplicate Values - 2 Columns

Hybrid View

hpaum99 Removing Duplicate Values - 2... 03-16-2010, 10:35 PM
Leith Ross Re: Removing Duplicate Values... 03-16-2010, 11:01 PM
JBeaucaire Re: Removing Duplicate Values... 03-16-2010, 11:11 PM
Leith Ross Re: Removing Duplicate Values... 03-16-2010, 11:53 PM
JBeaucaire Re: Removing Duplicate Values... 03-17-2010, 06:49 AM
hpaum99 Re: Removing Duplicate Values... 03-18-2010, 04:29 PM
Leith Ross Re: Removing Duplicate Values... 03-18-2010, 04:44 PM
hpaum99 Re: Removing Duplicate Values... 03-21-2010, 05:18 PM
JBeaucaire Re: Removing Duplicate Values... 03-21-2010, 05:22 PM
hpaum99 Re: Removing Duplicate Values... 03-22-2010, 12:14 AM
JBeaucaire Re: Removing Duplicate Values... 03-22-2010, 12:44 AM
hpaum99 Re: Removing Duplicate Values... 03-23-2010, 02:10 PM
JBeaucaire Re: Removing Duplicate Values... 03-23-2010, 04:39 PM
hpaum99 Re: Removing Duplicate Values... 03-27-2010, 12:47 AM
JBeaucaire Re: Removing Duplicate Values... 03-27-2010, 12:00 PM
  1. #1
    Registered User
    Join Date
    03-16-2010
    Location
    Delano, CA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Removing Duplicate Values - 2 Columns

    I have an excel file with 12,000 rows containing serial numbers and dates.

    The serial numbers are duplicated throughout the sheet as they have multiple dates. How can I delete duplicate serial number values, but keep the latest date value?

    I have attached an example excel file.

    Thanks!
    Attached Files Attached Files
    Last edited by hpaum99; 03-27-2010 at 11:54 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Removing Duplicate Values - 2 Columns

    Hello hpaum99,

    Welcome to the Forum!

    Do you have just these 2 columns in original workbook as well?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Removing Duplicate Values - 2 Columns

    Howdy neighbor! How's the weather up there in Delano?

    Try this:

    Option Explicit
    
    Sub RemoveDupes()
    Dim LR As Long
    LR = Range("A" & Rows.Count).End(xlUp).Row
    
    Range("A2:B" & LR).Sort Key1:=Range("A3"), Order1:=xlAscending, Key2:=Range("B3"), _
        Order2:=xlAscending, Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal
        
    Range("C2") = "key"
    Range("C3:C" & LR).FormulaR1C1 = "=IF(RC1=R[1]C1, """", 1)"
    Range("A2:C" & LR).AutoFilter Field:=3, Criteria1:="1"
    
    Range("A2:C" & LR).SpecialCells(xlCellTypeVisible).Copy Range("A" & LR + 2)
    Range("A2:C" & LR).AutoFilter Field:=3
    Range("A1:C" & LR).Delete xlShiftUp
    Range("C:C").ClearContents
    
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Removing Duplicate Values - 2 Columns

    Hello hpaum99,

    This macro is not effected by the 8192 cell limit imposed by the SpecialCells method. A button has been added to the sheet to run the macro.

    Sub DeleteDuplicates()
    
      Dim Data() As Variant
      Dim I As Long, N As Long
      Dim Rng As Range
      Dim RngEnd As Range
      Dim SN As Object
      Dim Wks As Worksheet
      
        Set Wks = Worksheets("Sheet2")
        
        Set Rng = Wks.Range("A3:B3")
        Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
        Set Rng = IIf(RngEnd.Row < RngRow, Rng, Wks.Range(Rng, RngEnd))
        
        Rng.Sort _
            Key1:=Rng.Item(1).Offset(-1, 0), Order1:=xlAscending, _
            Key2:=Rng.Item(2).Offset(-1, 0), Order2:=xlDescending, _
            Header:=xlYes, MatchCase:=False, Orientation:=xlTopToBottom
            
        ReDim Data(1 To Rng.Rows.Count, 1 To 2)
        
        Set SN = CreateObject("Scripting.Dictionary")
        SN.CompareMode = vbTextCompare
        
        Application.ScreenUpdating = False
        CalcMode = Application.Calculation
        Application.Calculation = xlCalculationManual
        
          For I = 1 To Rng.Rows.Count
            Key = Rng.Item(I, 1)
            If Key <> "" Then
              If Not SN.Exists(Key) Then
                 SN.Add Key, Rng.Item(I, 2)
                 N = N + 1
                 Data(N, 1) = Rng.Item(I, 1)
                 Data(N, 2) = Rng.Item(1, 2)
              Else
                 Rng.Item(I, 1).Resize(1, 2).Value = ""
              End If
            End If
          Next I
          
        Rng.Value = Data()
        
        Application.Calculation = CalcMode
        Application.ScreenUpdating = False
          
    End Sub
    Attached Files Attached Files

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Removing Duplicate Values - 2 Columns

    Leith, remember that the Special Cells limitation is 8192 areas, not cells. That can make a distinct difference. FYI.

  6. #6
    Registered User
    Join Date
    03-16-2010
    Location
    Delano, CA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Removing Duplicate Values - 2 Columns

    Leith - I do have these columns (Columns D & N) in my sheet. There are multiple columns in the sheet, those are just the 2 that I wanted to focus on. Do I just copy that control over to my excel file and run it?

    J - weather's good today! I'm new at this kind of excel work, I don't even know what to do with that code haha


    thanks for the help!! i really appreciate it

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Removing Duplicate Values - 2 Columns

    Hello hpaum99,

    Adding the Macro
    1. Copy the macro above pressing the keys CTRL+C
    2. Open your workbook
    3. Press the keys ALT+F11 to open the Visual Basic Editor
    4. Press the keys ALT+I to activate the Insert menu
    5. Press M to insert a Standard Module
    6. Paste the code by pressing the keys CTRL+V
    7. Make any custom changes to the macro if needed at this time.
    8. Save the Macro by pressing the keys CTRL+S
    9. Press the keys ALT+Q to exit the Editor, and return to Excel.

    To Run the Macro...
    To run the macro from Excel, open the workbook, and press ALT+F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

  8. #8
    Registered User
    Join Date
    03-16-2010
    Location
    Delano, CA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Removing Duplicate Values - 2 Columns

    Leith,

    Thanks!! That was easy enough..

    I ran the script, however I have more columns in my actual sheet. In the example I only posted 2 columns to make it easy, what do I need to change in the script so that the entire row is removed? The script is only affecting the cells in column a & b. The sheet goes up to Column W. There are duplicate values in all other columns but they can remain.


    thanks,
    Hiren

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Removing Duplicate Values - 2 Columns

    Mine updated, not sure if you've tried it or not, you may have missed it above originally:

    Option Explicit
    
    Sub RemoveDupes()
    Dim LR As Long
    Application.ScreenUpdating = False
    LR = Range("A" & Rows.Count).End(xlUp).Row
    
    Range("A2:Z" & LR).Sort Key1:=Range("A3"), Order1:=xlAscending, Key2:=Range("B3"), _
        Order2:=xlAscending, Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal
        
    Range("EE2") = "key"
    Range("EE3:EE" & LR).FormulaR1C1 = "=IF(RC1=R[1]C1, """", 1)"
    Range("A2:Z" & LR).AutoFilter Field:=3, Criteria1:="1"
    
    Range("A2:Z" & LR).SpecialCells(xlCellTypeVisible).Copy Range("A" & LR + 2)
    Range("A2:Z" & LR).AutoFilter Field:=3
    Range("A1:Z" & LR).Delete xlShiftUp
    Range("EE:EE").ClearContents
    
    Application.ScreenUpdating = True
    End Sub

  10. #10
    Registered User
    Join Date
    03-16-2010
    Location
    Delano, CA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Removing Duplicate Values - 2 Columns

    J - I ran the script and it gave me an error. I used the same steps that are in Leith's message.

    Run time error '1400'

    Sort Method of Range Class Failed.


    When I debug, this code is highlighted:

    Range("A2:Z" & LR).Sort Key1:=Range("A3"), Order1:=xlAscending, Key2:=Range("B3"), _
    Order2:=xlAscending, Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
    Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal


    thanks

  11. #11
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Removing Duplicate Values - 2 Columns

    Post up your actual workbook (dummy out the data if you need) so we can see why it acts differently in this workbook.

  12. #12
    Registered User
    Join Date
    03-16-2010
    Location
    Delano, CA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Removing Duplicate Values - 2 Columns

    here you go!
    Attached Files Attached Files

  13. #13
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Removing Duplicate Values - 2 Columns

    Ah, see...significantly different. Having an Excel "List" already active makes this a much different creature.

    This version will remove the list so the macro can edit the data, then reapply the list for you.
    Option Explicit
    
    Sub RemoveDupes()
    Dim LR As Long
    Dim oLo As ListObject, lName As String
    
    For Each oLo In ActiveSheet.ListObjects
        lName = oLo.Name
        Exit For
    Next oLo
    
    Application.ScreenUpdating = False
    LR = Range("A" & Rows.Count).End(xlUp).Row
    
    'Remove LIST
        If Len(lName) > 0 Then ActiveSheet.ListObjects(lName).Unlist
    
    'Sort Data
        Range("A2:Z" & LR).Sort Key1:=Range("A3"), Order1:=xlAscending, Key2:=Range("B3"), _
            Order2:=xlAscending, Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
            Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal
        
    'Remove duplicates
        Range("EE1") = "key"
        Range("EE2:EE" & LR).FormulaR1C1 = "=IF(RC1=R[1]C1, 1, """")"
        Range("EE:EE").AutoFilter Field:=1, Criteria1:="1"
        
        Range("A2:Z" & LR).SpecialCells(xlCellTypeVisible).Delete xlShiftUp
        ActiveSheet.AutoFilterMode = False
        Range("EE:EE").ClearContents
    
    'Reapply LIST
        LR = Range("A" & Rows.Count).End(xlUp).Row
        ActiveSheet.ListObjects.Add(xlSrcRange, Range("C1:W" & LR), , xlYes).Name = "List1"
    
    Application.ScreenUpdating = True
    End Sub

    Also, in the future, just start with a sample workbook more indicative of your needs. The initial workbook not only had no active LIST, the data was positioned differently. We could probably get it done in 1-2 posts with an accurate sample workbook.
    Attached Files Attached Files

  14. #14
    Registered User
    Join Date
    03-16-2010
    Location
    Delano, CA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Removing Duplicate Values - 2 Columns

    That worked perfectly.

    Thank you very much for the help! Appreciate it. I'll be sure to give full details next time.

  15. #15
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Removing Duplicate Values - 2 Columns

    Glad it worked out!

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to 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