+ Reply to Thread
Results 1 to 5 of 5

Delete duplicates based on two collumns

Hybrid View

  1. #1
    Registered User
    Join Date
    06-19-2014
    Location
    Montreal
    MS-Off Ver
    Office 365
    Posts
    12

    Delete duplicates based on two collumns

    Hi,

    I have a big spreadsheet containing costumers purshases for a period. The same customer generally purshased the same product more than once. I need to eliminate all the duplicate lines based with the same value on 2 collumns, the customer (B) and the product (D) in order to keep only one line for each product that the customers purshased.

    Thanks for helping...

    Simon
    Attached Files Attached Files

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,022

    Re: Delete duplicates based on two collumns

    Here are two methods. First one uses a formula in Col K, paste and drag down. Then in conjunction with the first macro listed
    below, will remove dupes.

    The second method (second macro) only requires a command button on Sheet Feuil2 and no formulas.

    Option Explicit
    
    ''###### MACRO ONE BEGINS #####
    'Delete dupes by formula & this macro
    'First, paste this formula into K2, then drag down : =B2&D2
    'Next paste a command button on sheet Feuil2 and attach to this macro :
    
    Sub text()
    Dim j As Integer, k As Integer, r As Range
    j = Range("K2").End(xlDown).Row
        For k = j To 2 Step -1
        'MsgBox k
        Set r = Range(Cells(k, "K"), Cells(k, "K").End(xlUp))
            If WorksheetFunction.CountIf(r, Cells(k, "K")) > 1 Then
                Cells(k, "K").EntireRow.Delete
            End If
        Next k
    End Sub
    
    ''##### MACRO ONE ENDS #####
    
    
    ''##### MACRO TWO BEGINS #####
    'You can forego all of the above by pasting a command button on sheet Feuil2 attached
    'this macro:
    
    Sub TestForDups()
    
       Dim LLoop As Integer
       Dim LTestLoop As Integer
       Dim Lrows As Integer
       Dim LRange As String
       Dim LCnt As Integer
    On Error Resume Next
    Application.ScreenUpdating = False
       'Column values
          
       Dim LColB_1, LColD_1
       Dim LColB_2, LColD_2
    
       'Test first 400 rows in spreadsheet for duplicates (delete any duplicates found)
       Lrows = 40
       LLoop = 2
       LCnt = 0
    
       'Check first 40 rows in spreadsheet
       While LLoop <= Lrows
         
          LColB_1 = "B" & CStr(LLoop)
          LColD_1 = "D" & CStr(LLoop)
         
          If Len(Range(LColB_1).Value) > 0 Then
    
             'Test each value for uniqueness
             LTestLoop = LLoop + 1
             While LTestLoop <= Lrows
                If LLoop <> LTestLoop Then
                   LColB_2 = "B" & CStr(LTestLoop)
                   LColD_2 = "D" & CStr(LTestLoop)
                   
                  'Value has been duplicated in another cell (based on values in columns A to H)
                   If (Range(LColB_1).Value = Range(LColB_2).Value) _
                   And (Range(LColD_1).Value = Range(LColD_2).Value) Then
                    
                      'Delete the duplicate
                      Rows(CStr(LTestLoop) & ":" & CStr(LTestLoop)).Select
                      Selection.Delete Shift:=xlUp
    
                      'Decrement counter since row was deleted
                      LTestLoop = LTestLoop - 1
    
                      LCnt = LCnt + 1
    
                   End If
    
                End If
    
                LTestLoop = LTestLoop + 1
             Wend
    
          End If
    
          LLoop = LLoop + 1
       Wend
    
       'Reposition back on cell A1
       Range("L2").Select
       
    Application.ScreenUpdating = True
    End Sub
    
    ''##### MACRO TWO ENDS #####
    You will be able to view the dupes deletion as the yellow highlighted row will be deleted leaving the beige colored row.
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    06-19-2014
    Location
    Montreal
    MS-Off Ver
    Office 365
    Posts
    12

    Re: Delete duplicates based on two collumns

    Fantastik! Thanks so much... the idea of merging the cust num. + the product code and delete duplicates was so logic, I probably stayed to late at the office.... I'll keep this trick in mind!

    Thanks,

    Simon

  4. #4
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Delete duplicates based on two collumns

    Try this code
    Sub DeleteDuplicateRows()
        Dim rng As Range
        
        With ActiveSheet
            Set rng = .Range("A2:J" & .Cells(Rows.Count, 1).End(xlUp).Row)
            rng.RemoveDuplicates Columns:=Array(2, 4), Header:=xlNo
        End With
    End Sub
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  5. #5
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Delete duplicates based on two collumns

    You're welcome. Glad we can offer some help for you
    Thanks for the feedback and for the rep points

+ 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. Delete Duplicates based on rule
    By hzarry in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-05-2016, 10:26 AM
  2. How to delete collumns range
    By Diamon in forum Excel General
    Replies: 4
    Last Post: 08-31-2015, 11:35 AM
  3. [SOLVED] Delete Duplicates based on Criteria
    By yuenk in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-10-2014, 11:56 AM
  4. Delete Duplicates based on Criteria
    By yuenk in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-10-2014, 11:48 AM
  5. [SOLVED] Delete duplicates based on criteria
    By chrism_cfu in forum Excel General
    Replies: 6
    Last Post: 12-07-2012, 08:52 AM
  6. [SOLVED] Delete duplicates based on DATES.
    By marreco in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-31-2012, 06:26 AM
  7. Delete duplicates based on cell value
    By uncleslinky in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-15-2011, 12:01 PM

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