+ Reply to Thread
Results 1 to 14 of 14

Macro to find rows with duplicates, compare cells between them and delete rows.

  1. #1
    Registered User
    Join Date
    06-28-2011
    Location
    Spain
    MS-Off Ver
    Excel 2003
    Posts
    29

    Macro to find rows with duplicates, compare cells between them and delete rows.

    Hello world,

    This is my first post, please let me know if my question is not clear.

    I am trying to write a macro to find duplicated in column F, see if they have matching values for column O and then delete all rows except for the one that has the longest string in column B.

    I think I need to do the following (starting with value in F1 and finishing at the end of the column):

    1) Look for duplicates in all column F
    2) For each duplicate: check if values in column O are also duplicates
    3) If the values in column O are duplicates, then delete all rows except for the one that has the most data in column B.

    I am not sure how I should start writing a solution for this.

    I know I can use the =LEN() function to check the number of characters in a cell.

    However, I am not sure how to pass on the cell references of the duplicates in F so that I can search for duplicates in O. Once I have those references I can pass them on to check for the length of the strings in column B as well.

    Then, to delete the rows would I use the row reference and EntireRow.Delete?

    I have attached a spreadsheet with an example of what I am trying to achieve.

    I have been browsing the forums for a long time but have never asked a question before.

    Thanks for your help.
    Attached Files Attached Files
    Last edited by vzc8; 06-28-2011 at 11:33 AM.

  2. #2
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Macro to find rows with duplicates, compare cells between them and delete rows.

    What happens if the rows to be deleted in "B" have text of the same length ???

  3. #3
    Registered User
    Join Date
    06-28-2011
    Location
    Spain
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: Macro to find rows with duplicates, compare cells between them and delete rows.

    Thanks for the quick reply MickG.

    Sorry, I forgot to say: If the values in column B are the same then delete all duplicates.

  4. #4
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Macro to find rows with duplicates, compare cells between them and delete rows.

    What I realy meant was, if there were 3 duplicates in "B", the length of data in one cell was 3, and the other two where 6, which cell wants deleting.

  5. #5
    Registered User
    Join Date
    06-28-2011
    Location
    Spain
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: Macro to find rows with duplicates, compare cells between them and delete rows.

    Thanks again MickG.

    Let's say the following rows have identical values in column F and O and have the following in column B:

    1 ABCDEF
    2 ABCDEF
    3 ABC

    Then row 2 (ABCDEF) and row 3 (ABC) should be deleted and row 1 (the first in the list of duplicates and the one with the longest, or one of the longest, values in B) should be saved.

    Does that make sense, I am not sure if I am explaining myself so well or if what I am trying to do is even possible.

    I really appreciate your help though.

    Thanks.

  6. #6
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Macro to find rows with duplicates, compare cells between them and delete rows.

    Try this:-
    I've amended the code I produced yesterday, as I don't think it was correct !!
    Please Login or Register  to view this content.
    Regards Mick
    Last edited by MickG; 06-30-2011 at 06:06 AM.

  7. #7
    Registered User
    Join Date
    06-28-2011
    Location
    Spain
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: Macro to find rows with duplicates, compare cells between them and delete rows.

    MickG,

    Thanks, your help has been great.

    I have been looking at your code and trying to understand it, but unfortunately I do not understand it all. I am learning how to program macros, but some of this has got me confused.

    The first version you uploaded did delete rows in case of duplicates, but it seems to have been looking for duplicates in column B and then deleting everything else (see example attached).

    The edited code is causing "Error 91 Object Variable or With block variable not set", I am not sure why, when I click debug it highlights the penultimate line: nRng.EntireRow.Delete

    With the old version it was almost working correctly, but in some cases it seems to delete rows even when it should now.

    In rows 12 & 13 (see attached file), the old macro deleted both rows, but should in fact keep the one with the longest value in column B.

    So:
    12333 | RG | 622e76m00007
    12334455 | RG | 622e76m00007

    should turn into:
    12334455 | RG | 622e76m00007

    I have attached a new file of examples and have included the macro that is causing the 91 error. The macro has been called "TidyUpExtract".

    "upload(30Jul11_fortesting).xls" is the file that contains the original data and "upload(30Jul11_examples).xls" contains the data with both the old macro output and the intended output.

    Thanks again for any help that you can offer.
    Attached Files Attached Files

  8. #8
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Macro to find rows with duplicates, compare cells between them and delete rows.

    Hi, This is the result of Modified code below.
    NB:- In your previous thread you said that if any sets of cells for deletion in column "B" had values of the same length and also had the longest lengths, then remove all except one of them, That what my codes does , but you results show more than one.
    As in 1234567890 which My code returns 2 and your 3.

    Please Login or Register  to view this content.
    New Code:-
    Please Login or Register  to view this content.
    Regards Mick
    Last edited by MickG; 06-30-2011 at 12:08 PM.

  9. #9
    Registered User
    Join Date
    06-28-2011
    Location
    Spain
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: Macro to find rows with duplicates, compare cells between them and delete rows.

    MickG: Thank you very much!

    So far I think it is a 100% fit for what I am trying to accomplish. I was testing it yesterday and meant to reply but ran out of time, sorry about that.

    I am impressed with you quick reply and really appreciate you help. I am still learning to program macros, and have to admit that I could never have written this on my own.

    Thanks for posting this solution, I will try to learn as much as I can from studying this code.

  10. #10
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Macro to find rows with duplicates, compare cells between them and delete rows.

    Thank you for your kind responses, they are much appreciated
    Regards Mick

  11. #11
    Registered User
    Join Date
    06-28-2011
    Location
    Spain
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: Macro to find rows with duplicates, compare cells between them and delete rows.

    Hello again,

    Sorry, but I have a follow-up question. After running the code above I need to go back, inspect the file and then clean for values in F that have an identical value in B.

    So:

    B F
    1234 ABCD
    12345 ABCDE
    123 ABCD
    123 ABCD
    1234 ABCD

    Should become:
    B F
    1234 ABCD
    12345 ABCDE
    123 ABCD

    I took the code you wrote above and edited only a minor part:

    Please Login or Register  to view this content.
    Is that a correct and sensible way of doing that? It seems to work, but I am not sure it is the best way.

    Also, I realized that I need to run another check after cleaning the data. I need to compare values in F to see if a cell that contains another cell's data also has the same data of column O, and if they do I need to delete the row with the shortest value in F.

    Example:

    F O
    Bart Bevers 12345
    Bevers 12345

    Would become:

    F O
    Bart Bevers 12345

    I was looking at it and it seems like I could recycle the code from the original macro, but I am not sure how to best go about checking for if values in a cell in F are found in another cell in F.

    Thanks, any help is appreciated.

  12. #12
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Macro to find rows with duplicates, compare cells between them and delete rows.

    Try this for the first :-
    Please Login or Register  to view this content.
    and this for the second:-
    Please Login or Register  to view this content.
    Regards Mick

  13. #13
    Registered User
    Join Date
    06-28-2011
    Location
    Spain
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: Macro to find rows with duplicates, compare cells between them and delete rows.

    Thanks again MickG. Sorry for the slow reply, I was gone the last few days.

    This is great, the code works exactly the way I wanted it to. I still have some way to go myself.

    Again, you have been an incredible source of help. It is much appreciated.

  14. #14
    Registered User
    Join Date
    06-28-2011
    Location
    Spain
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: Macro to find rows with duplicates, compare cells between them and delete rows.

    Mod: this thread can be marked solved. Thanks.

+ 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