+ Reply to Thread
Results 1 to 9 of 9

Compare two columns and return list of duplicates

  1. #1
    Forum Contributor
    Join Date
    02-09-2010
    Location
    Constanta
    MS-Off Ver
    Excel 2007
    Posts
    128

    Compare two columns and return list of duplicates

    Greetings,

    I have a file containing two worksheets. Sheet1 contains data (line details for every contract number) that I download from a website. This data is then updated via macro to Sheet2, which is basically the database for all data that has been downloaded.

    In order to avoid duplicate update of the database, I would like to incorporate some code in the existing macro that will do the following:

    1. Check the contract number in Sheet1 to see if this number already exists in Sheet2
    2. Display via a message box / any other dialog box the duplicate contract numbers and then exit the macro.

    If it helps, I have attached a sample file.

    Many thanks in advance
    Asha
    Attached Files Attached Files
    Last edited by asha3010; 06-15-2010 at 06:45 AM.

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Compare two columns and return list of duplicates

    Hi asha3010

    What's the key that makes an item a duplicate? Columns C and D of each sheet? Line 1 of sheet 1 appears to be a duplicate but the Amt is different from sheet 2 Amt.

    John
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Forum Contributor
    Join Date
    02-09-2010
    Location
    Constanta
    MS-Off Ver
    Excel 2007
    Posts
    128

    Re: Compare two columns and return list of duplicates

    Hello jaslake,

    Thanks for your response.

    What's the key that makes an item a duplicate? Columns C and D of each sheet?


    Col D of each sheet contains the unique key. For eg, the code should not allow me to proceed with the update since Row2 in Sheet1 has a contract number that already exists in Sheet2.

    Line 1 of sheet 1 appears to be a duplicate but the Amt is different from sheet 2 Amt.


    You are right. That was a typo - sorry about it.

    Hope this clarifies.

    Many thanks again
    Asha

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Compare two columns and return list of duplicates

    Hi asha3010

    I'm confused. Worksheet 2, Col D has duplicates yet you've said
    Col D of each sheet contains the unique key
    Based on my understanding of your requirements, line 2 of Worksheet 1 would be rejected and line 4 of Worksheet 1 would be rejected; line 3 of Worksheet 1 would be accepted. Is this correct?

    John

  5. #5
    Forum Contributor
    Join Date
    02-09-2010
    Location
    Constanta
    MS-Off Ver
    Excel 2007
    Posts
    128

    Re: Compare two columns and return list of duplicates

    Hi John,

    Let me elaborate. Sheet1 contains data that is downloaded from a website. Once this data is updated (via macro) to Sheet2, the same macro clears all data in Sheet1.

    What I wanted to prevent is me accidentally downloading again the same data into Sheet1 without realising that it has already been updated to Sheet2. Hence, I wanted to check if the Contract Number in Sheet1 already exists in Sheet2.

    So row2 in Sheet1 should be rejected but rows 3-4 should be accepted.

    Hope this clarifies or is it as clear as mud?

    Thanks so much for your patience.
    Asha

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Compare two columns and return list of duplicates

    Hi asha3010

    That helps. I'll look into it for you. I'll be traveling today.

    John

  7. #7
    Forum Contributor
    Join Date
    02-09-2010
    Location
    Constanta
    MS-Off Ver
    Excel 2007
    Posts
    128

    Re: Compare two columns and return list of duplicates

    Thanks John. Safe travels!

  8. #8
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Compare two columns and return list of duplicates

    Hi asha3010

    Try this code
    Please Login or Register  to view this content.
    The code first finds duplicate contract numbers and flags them. It then adds all unflagged contract numbers to sheet 2 and presents a message of duplicate contracts. Let me know of issues.

    John
    Attached Files Attached Files

  9. #9
    Forum Contributor
    Join Date
    02-09-2010
    Location
    Constanta
    MS-Off Ver
    Excel 2007
    Posts
    128

    Re: Compare two columns and return list of duplicates

    Hello John,

    Many thanks for your prompt response. Sorry for not checking the code earlier.

    Your code works great. I tweaked it a bit - used the countif function since the original worksheet is protected & will not allow for any column inserts.

    I have attached the revised file if you would like to see the countif code.

    Many thanks again for your support.

    Regards
    Asha
    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)

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