+ Reply to Thread
Results 1 to 17 of 17

Compare multiple columns and delete duplicates

  1. #1
    Registered User
    Join Date
    10-24-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    45

    Compare multiple columns and delete duplicates

    I found the following code for comparing worksheets. What I need to do is update it so that it compares 2 columns (column A and column B) of data on each worksheet, and deletes the duplicate rows from worksheet 'New'.

    A record is only a duplicate if it matches in both columns on the corresponding row i.e. if there is a match in cell A27, the other match must be cell B27, not another cell in column B.

    Thanks for your help

    Please Login or Register  to view this content.
    Last edited by labrooy; 11-03-2011 at 08:06 PM.

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

    Re: Compare multiple columns and delete duplicates

    Please attach a sample workbook so we can help you out better. Also, will the duplicates be one tab or on 2 separate tabs that need to be compared?

  3. #3
    Registered User
    Join Date
    10-24-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    45

    Re: Compare multiple columns and delete duplicates

    Sample file attached.

    Worksheet "New" needs to compare to worksheet "Master" and any duplicates/matches are to be removed from worksheet "New".

    The data will change and increase each day. .
    Attached Files Attached Files
    Last edited by labrooy; 10-25-2011 at 01:18 AM. Reason: Removed redundant text (there is no longer any blank cells in column B of 'Master') and updated sample file

  4. #4
    Registered User
    Join Date
    10-24-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    45

    Re: Compare multiple columns and delete duplicates

    I have managed to write the code, using a combination of some vba I found on this site, and some Macro recording. This could ultimately be a very large file, so I would appreciate it if some one could optimise this.


    Please Login or Register  to view this content.

  5. #5
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Compare multiple columns and delete duplicates

    Try it.
    Please Login or Register  to view this content.
    In the attached file, click the green arrow.
    Attached Files Attached Files

  6. #6
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Compare multiple columns and delete duplicates

    Perhaps a macro using AutoFilter could also be of interests?

    Please Login or Register  to view this content.
    Alf

  7. #7
    Registered User
    Join Date
    10-24-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    45

    Re: Compare multiple columns and delete duplicates

    Thanks for the feedback. I have tested the macro using an autofilter from this site. It seemed to be slower than the existing solution, though that was with a small sample of 500 rows. I expect that going forward I will incorporate the autofilter.

    I will test both of your code to see how they work.

    Alf, one thing I have noticed - the data being compared in worksheet Master is in columns A and B, my novice glance at your code suggests you may only be referring to column A.

  8. #8
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Compare multiple columns and delete duplicates

    That's because in your uploaded file if a number in the "Dev" column of the Master sheet match a number in the "Dev" column of the New sheet they both have the same "NM" number.

    Are you now telling me that for the same "Dev" number in Master and New sheets you can have different "NM" numbers?

    Alf

  9. #9
    Registered User
    Join Date
    10-24-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    45

    Re: Compare multiple columns and delete duplicates

    Absolutely, it is possible to have different NM numbers for the same Dev number.

    Hence my macro must check both columns for a match.

  10. #10
    Registered User
    Join Date
    10-24-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    45

    Re: Compare multiple columns and delete duplicates

    Hi Nilem,

    I just ran your macro, it did not work how I would like it to. On your file it doesn't appear to make any changes to the existing sample data.

    I added it to my sample file, and it removed all data on sheet 'New' regardless of whether or not it matched the data on sheet 'Master'.

    There was also a Run-time error '1004': Application-defined or object-defined error for the last line
    .Range("A2:B2").Resize(j).Value = y:

  11. #11
    Registered User
    Join Date
    10-24-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    45

    Re: Compare multiple columns and delete duplicates

    Here is the latest version of my code, with an autofilter included.

    Please Login or Register  to view this content.

  12. #12
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Compare multiple columns and delete duplicates

    Hence my macro must check both columns for a match
    Piece of cake code modified.

    Please Login or Register  to view this content.
    Alf
    Last edited by ConneXionLost; 11-02-2011 at 05:46 PM. Reason: Closing tag fixed

  13. #13
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Compare multiple columns and delete duplicates

    Just testing code tags:

    Please Login or Register  to view this content.
    Now it works properly.

    Alf

  14. #14
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Compare multiple columns and delete duplicates

    You had forgotten the "/" in the ending code tag so it didnt work.

  15. #15
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Compare multiple columns and delete duplicates

    Hi arlu1201

    You had forgotten the "/" in the ending code tag so it didnt work.
    Thanks I must have deleted the "/" without noticing.

    Alf

  16. #16
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Compare multiple columns and delete duplicates

    Hi labrooy

    After some more testing of my macro code I found a bug. Word of warning as this macro contains an "Activesheet.Usedrange" comand. It works ok in Excel 2003 but not in Excel 2007.

    Please Login or Register  to view this content.
    Alf

  17. #17
    Registered User
    Join Date
    10-24-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    45

    Re: Compare multiple columns and delete duplicates

    Thanks Alf, I also noticed an issue with the former code, it now works well.

+ 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