+ Reply to Thread
Results 1 to 15 of 15

Deleting Duplicate Rows???

  1. #1
    Registered User
    Join Date
    04-10-2007
    Location
    Marietta, GA
    Posts
    25

    Deleting Duplicate Rows???

    I am hoping someone can help me. I have a spreadsheet that I need to delete duplicate rows in. However, in order to determine if a row is a duplicate I need to check 2 cells per row. In the attached file you will see that each row has 4 cells. I need to compare the cells in columns B and C with the B and C cells of the Row beneath. If the B and C cells match then it is considered a duplicate and one of the rows needs to be deleted. Also, there may be multiple duplicate rows.

    For an example see rows 17, 18 and 19. I only need 1 row to remain.

    I am looking for a vb script that would analyze a file with thousands of rows and delete the duplicates. Any feedback or suggestions would be greatly appreciated.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    delete duplicates based on 2 cell comparison

    Hi - the below code will systematically take each 2 cell pair and compare to all remaining 2 cell pairs, deleting duplicates as it goes (I assumed your list of data was not sorted, but this will work even if it is).

    Please Login or Register  to view this content.
    If you have blank rows in your data then the CurrentRegion property may not work, even after you adjust your start row per the comment. You could use this to get the last row of data:

    Please Login or Register  to view this content.
    But - if you have things like blanks in subsequent rows then (without if conditions to ignore matches on blanks) these too would be deleted. depending on the nature of your data, you might find a better "special cell" definition, based on format or something else ... have a look at the online help if this is a problem ..

    Hope this helps ... MM.

  3. #3
    Registered User
    Join Date
    09-04-2008
    Location
    florida
    Posts
    16
    Hi MM,

    What if you have columns and you have to compare 4 or 5 rows in it?

    Thanks.

  4. #4
    Registered User
    Join Date
    09-04-2008
    Location
    florida
    Posts
    16
    I am sorry. I meant a different thing. What I was trying to ask was - what if you have 8 columns and you have to compare 4-5 columns?

  5. #5
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    different problem?

    Hi Mia .. I'm not sure if this is a different problem or not ... the code below will delete multiple duplicate rows based on whether or not the cells in 2 columns are the same for the rows being compared ...

    If this isn't what you mean, then I suggest you start a new post and explain what you're trying to do

    MM.

  6. #6
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    more columns ..

    Ah - I see ... this could be expanded, but you'd have to tell me what your match criteria is .. is it based on 4 columns or 5 or if it's sometimes 4 and sometimes 5 then what are the conditions?

    MM.

  7. #7
    Registered User
    Join Date
    04-10-2007
    Location
    Marietta, GA
    Posts
    25
    I have tried to run the macro a couple of times but in both cases it runs continuously and eventually Excel stops responding. The file I am deleting dupes in is 70,000 rows long. I think this may be causing the problem because when I run the macro on a much smaller file (100 rows) it seems to work ok. Is there anything you would suggest other than breaking the data into smaller pieces? Is there any way to rewrite the script such that it would not get so bogged down?

    I am running this on a relatively new machine with 4gigs of memory so I don't think it's the hardware.

  8. #8
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862
    Hi - sorry about that ... I didn't realise your dataset was so large; if you send me a sample of the data I'll try to optimise it .. and no - it won't be your hardware :-) MM.

  9. #9
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    new improved ...

    Hi jerickson .. I've reworked this slightly so that it will now run more efficiently; i've tested it on 5000 lines and it runs in a few seconds.

    Please Login or Register  to view this content.
    Let me know how you go. MM

  10. #10
    Registered User
    Join Date
    04-10-2007
    Location
    Marietta, GA
    Posts
    25
    That did the trick! Thanks MM for your help.

    One thing I wanted to ask. I had sorted the data in A->Z order prior to running the script for Column A then B then C. After I ran the script the data was in a strange order and I had to sort it again. Is that expected? Should I be concerned about data loss? I'm pretty certain it's working fine but I thought the way it altered the order was strange. If it's working then it's not a big deal as I can just add a sort to the end of the script. I was just curious why it changed the order.

  11. #11
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    sort order ..

    Hi - glad that works for you:-)

    I had to run a sort on B then C prior to reading in the data so that it didn't need to select every value and check it against every other value; sorting it means i know when the next value is not the same i can stop searching any remaining rows. Make sense?

    In any case, if you add the following at the end, your sort order will be restored:
    Please Login or Register  to view this content.
    Cheers, MM.

  12. #12
    Registered User
    Join Date
    04-10-2007
    Location
    Marietta, GA
    Posts
    25
    That does make sense...thanks again. BTW, my wife and I used to live in Sutton Coldfield outside of Birmingham. Work transfer.

  13. #13
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862
    Excellent - glad that's done the trick:-) I'd like to say "nice to hear from a fellow Englishman .." but I'm from Oz ... work transfer too. Not enjoying what you chaps generously refer to as "summer" here, but the proximity to Europe more than makes up for it. All the best & good luck. MM.

  14. #14
    Registered User
    Join Date
    10-26-2012
    Location
    Dearborn Hts, MI
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Deleting Duplicate Rows???

    Can you make a similar vb script for my database.so in other words, I need the rows with the same addresses but with different names deleted to only have the one name and one address.

    I have a lot of duplicate addresses with different names. I just want to keep one name and one address.
    I have over 7000 entries that looks like this:
    _ A__________B___________C_______D____E______F
    1
    2 Last Name__First Name___Address__City__State__Zip
    3 Smith______John________123 A____Det__MI_____48227
    4 Smith______Joe_________123 A____Det__MI_____48227
    etc.

    Thanks
    Jade
    Last edited by jadegreenwolf; 10-26-2012 at 01:52 PM.

  15. #15
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Deleting Duplicate Rows???

    Hello jadegreenwolf, and welcome to the forum.

    Unfortunately you have inadvertently broken one of the forum rules. Please read the following and make the necessary change. Thanks.

    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. It makes sense to have a new thread for your question because a thread with numerous replies can be off putting & difficult to pick out relevant replies.

+ 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. Finding and deleting both duplicate rows
    By udfxrookie in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 02-17-2011, 05:41 PM
  2. Deleting duplicate offsetting rows
    By bshbros in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-23-2008, 05:30 PM
  3. Deleting duplicate rows
    By PBANKS in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-17-2008, 03:57 PM
  4. Deleting both duplicate rows in Excel.
    By mdub72 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-06-2008, 08:35 AM
  5. deleting duplicate rows based on 2 criteria
    By Zygoid in forum Excel General
    Replies: 1
    Last Post: 04-25-2007, 01:17 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