Closed Thread
Results 1 to 15 of 15

How to find duplicate entries, find which has the lowest value, and delete the rest?

  1. #1
    Registered User
    Join Date
    03-05-2010
    Location
    Singaproe
    MS-Off Ver
    Excel 2007
    Posts
    18

    How to find duplicate entries, find which has the lowest value, and delete the rest?

    Hi guys,

    I have a spreadsheet which lists all the price changes for the month.
    Before doing any updates, I will need to search through the spreadsheet for any duplicates.

    If any duplicate rows are found, I need the VBA code to look at the 6th column of those duplicate rows to find out which duplicate row has the lowest value, and delete the rest of the duplicate rows.

    For e.g. If there are 4000 rows, and duplicates are found in row 10, 100 and 1000, the code will find that row 100 has the lowest value in the accompanying cell in the 6th column, and delete row 10 and row 1000.

    So in the end there will only be 3999 rows left, with no blank rows in between.

    I think I would need to use arrays right? I'm still not good with arrays and looking up on it now.

    Will appreciate any advice given!

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

    Re: How to find duplicate entries, find which has the lowest value, and delete the re

    Can you post up a sample workbook with 30-40 rows of sample data so we can see clearly what is considered "duplicate".

    Also, can the data be sorted prior to evaluation or does the data have to be evaluated with all the rows in the exact order they already exist? It's much less work to do these comparisons if the data can be sorted, quite simple in fact since we can sort by the key column AND by that column F so the lowest value is always at the top in the group of sorted values.

    Click GO ADVANCED and use the paperclip icon to post up your workbook.
    _________________
    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!)

  3. #3
    Registered User
    Join Date
    03-05-2010
    Location
    Singaproe
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: How to find duplicate entries, find which has the lowest value, and delete the re

    Hi JBeaucaire,

    Yes, the column will be sorted alphabetically, and I can sort the columns in column 6 in ascending order too.

    I have attached the file here.

    I am trying out the following code, but have trouble getting to work properly yet. Not sure if its an optimal solution, but i think we can start from here if you wish.

    Please Login or Register  to view this content.
    For the intFinalBillingRow, I have issues getting the code to recalculate the final row of the sheet after the duplicates are removed, as it will be used later. Any advice on this?

    Thanks in advance!
    Attached Files Attached Files

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

    Re: How to find duplicate entries, find which has the lowest value, and delete the re

    This is how I would do it, we only delete the unneeded rows once at the very end, so even on a larger dataset this should work quite fast.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    03-05-2010
    Location
    Singaproe
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: How to find duplicate entries, find which has the lowest value, and delete the re

    Thanks for the quick reply JBeaucaire, and WOW it worked beautifully!

    That's is one complex code...(to me)
    I don't understand this part of the code

    Please Login or Register  to view this content.
    How does it work?

    Also, do you have any suggestions to check if there are non-duplicate rows which are deleted? I'm not doubting your work, but I want to know the sequential thinking behind this.

    My logic sucks! LOL

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

    Re: How to find duplicate entries, find which has the lowest value, and delete the re

    That code really does the same thing your original code did, compare if there are like values adjacent and set the lower one to be deleted.

    The only difference is you delete over and over again as you went, which is quite inefficient. Instead, I use a declared Range and keep adding cells to the range using the Union() command.

    When the loop reaches the top, it has a very long list of cells in the Range, then we delete the entire row of all the cells in the range, once...all at once.

    There will be no non-duplicated rows in the Range. There is only one requirement for getting a row added to the list, it has to be adjacent to another row with the same value in column A. If the values aren't the same, it is skipped.

    ===========
    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  7. #7
    Registered User
    Join Date
    03-05-2010
    Location
    Singaproe
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: How to find duplicate entries, find which has the lowest value, and delete the re

    Thanks for the explanation JBeaucaire,

    I'm still running though the spreadsheets to make sure everything is fine.
    My master workbook has 16,000 rows, and I have 11 spreadsheets to check against with each spreadsheet running to 1000 rows each.

    Making sure the updates/deletions are accurate is a headache...if I'm checking it correctly in the first place.

    Once done, I'll change the status of this thread.

    Thanks again!

  8. #8
    Registered User
    Join Date
    07-22-2011
    Location
    Bay Area, US
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: How to find duplicate entries, find which has the lowest value, and delete the re

    Thank you for this code. Very helpful!

    I'd like to take the same example and add one additional column into the determination of which rows to keep. In this example I need to keep the lowest price for each combination of Column A and Column D. I've provided updated sample workbook.

    Thanks in advance.
    Attached Files Attached Files

  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: How to find duplicate entries, find which has the lowest value, and delete the re

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    07-22-2011
    Location
    Bay Area, US
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: How to find duplicate entries, find which has the lowest value, and delete the re

    Worked like a charm! Thanks a ton.

  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: How to find duplicate entries, find which has the lowest value, and delete the re

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  12. #12
    Registered User
    Join Date
    07-22-2011
    Location
    Bay Area, US
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: How to find duplicate entries, find which has the lowest value, and delete the re

    I'm not seeing a Prefix dropdown. Is that because I did not create the original post?

  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: How to find duplicate entries, find which has the lowest value, and delete the re

    Ouch, I didn't even notice you had hijacked someone else's thread. Tsk-tsk.

  14. #14
    Registered User
    Join Date
    10-31-2011
    Location
    TX, United States
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: How to find duplicate entries, find which has the lowest value, and delete the re

    I have the same problem. Could someone take a look at my attachment and let me know what i need to alter in the code above to make this work for my layout?

  15. #15
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: How to find duplicate entries, find which has the lowest value, and delete the re

    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

Closed 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