Closed Thread
Results 1 to 19 of 19

Delete duplicate rows based on all columns

  1. #1
    Registered User
    Join Date
    08-21-2008
    Location
    USA
    Posts
    27

    Delete duplicate rows based on all columns

    How do I delete duplicate rows in a sheet using a macro. When I say duplicate row, it is not based on a particular column but all the columns, so it is a true duplicate record.

  2. #2
    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: Delete duplicate rows based on all columns

    Hi,

    One way would be to use a couple of helper columns. In one, say column D, concatenate all the column values for a row (i.e. =A1&B1&C1) and copy this down. Then in the other helper column say column E enter the formula

    Please Login or Register  to view this content.
    Now filter column E for values > 1. These are all the duplicate records. Now just select all these rows and delete them.

    HTH
    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.

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

    Re: Delete duplicate rows based on all columns

    Richard's approach is how I would do it, too. My only caveat is you have to be aware of false matches.

    If A1 = 22
    B1 = 3

    Then concatenation formula =A1&B1 results in the text string "223"

    If A2 = 2
    B2 = 23

    Then concatenation formula =A2&B2 also results in the text string "223"

    This is a false match.

    While constructing your formula, by hand or by macro, consider putting a "spacer" between each field to keep the matches exact:

    =A1&"-"&B1
    ...results in "22-3" and "2-23" so no false matches.
    _________________
    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!)

  4. #4
    Registered User
    Join Date
    08-21-2008
    Location
    USA
    Posts
    27

    Re: Delete duplicate rows based on all columns

    Thank you for the reply. But I would like to automate it. since # of columns are too many and might increse or decrease.

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

    Re: Delete duplicate rows based on all columns

    This macro assumes the first row is "titles" and should be skipped. It requires the first row for the AutoFilter, so if your data really does start at row 1, add a blank row there.

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    08-21-2008
    Location
    USA
    Posts
    27

    Re: Delete duplicate rows based on all columns

    Thanks for the code. I ran it and the all the data got deleted. I am working on draft dont worry. But wondering if I need to change somethign in the code
    Last edited by DonkeyOte; 01-08-2010 at 05:13 AM. Reason: unnec. quote removed

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

    Re: Delete duplicate rows based on all columns

    Nope. Feel free to upload a sample sheet so I can see examples of your kind of data. It works fine on the sample sheets I have, so let's look at yours.

    Oh, and don't use the QUOTE button to reply, it just clutters the thread and makes finding your responses harder...unless of course there is something specific you want to point out...quote just that part. Otherwise, just use the QUICK REPLY box below.

  8. #8
    Registered User
    Join Date
    08-21-2008
    Location
    USA
    Posts
    27

    Re: Delete duplicate rows based on all columns

    Thank you for your suggestions. Here is what I am doing. I have a macro which concatenated sheets(with name "test") from all workbooks in a folder. In this code I had to specify the range. So I specified from first label row too. Since these worksheets in all the workbooks have same structure including same header row, I am getting multiple header rows(duplicate records). So I need to keep just the first one and delete the test duplicate rows. Using your code, I did 2 ways:
    1.Ran the code as is since the 1st row is header row(label row from the first source sheet). This way it is keeping the first one and deleting everything. SO finally I have only the first row.
    2.i have inserted a blank 1st row and then ran your macro. Its deleting the duplicate header rows but it is deleting the the first one as well.

  9. #9
    Registered User
    Join Date
    08-21-2008
    Location
    USA
    Posts
    27

    Re: Delete duplicate rows based on all columns

    If I run the worksheet from where i actually run the code for concatenating worksheets from workbooks in a folder, it creates a new workbook with the concatenated sheet. When i run this macro in this new sheet immediately this is not working at all. If I save this sheet else where and run this code it is working as in my previous post

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

    Re: Delete duplicate rows based on all columns

    I'd really like to help. Since the macro is working for me on my test sheets, I'll need to see your sample workbook.

    Click GO ADVANCED and use the paperclip icon to post up your workbook.

  11. #11
    Forum Contributor vijay2482's Avatar
    Join Date
    03-03-2009
    Location
    Paris,France
    MS-Off Ver
    Excel 2003
    Posts
    263

    Re: Delete duplicate rows based on all columns

    Hello JBeaucaire,

    I tried to use your code below but found the macro struck at the line:
    Please Login or Register  to view this content.
    My comparision has to start from the 5th row.
    I have changed ur code as follows:
    Please Login or Register  to view this content.
    Any help to fix it will be helpful.
    Thanks innAdvance.
    VIjay

    If you find any of the post useful to your question, please add to the contributor's reputation by clicking the blue scales icon in the blue bar of the post.

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

    Re: Delete duplicate rows based on all columns

    The change you made is meaningless. It shouldn't change the way the LR and LC values are being determined.

    Quote Originally Posted by JBeaucaire View Post
    I'd really like to help. Since the macro is working for me on my test sheets, I'll need to see your sample workbook.

    Click GO ADVANCED and use the paperclip icon to post up your workbook.

  13. #13
    Forum Contributor vijay2482's Avatar
    Join Date
    03-03-2009
    Location
    Paris,France
    MS-Off Ver
    Excel 2003
    Posts
    263

    Re: Delete duplicate rows based on all columns

    Thanks for your reply.
    Have attached a sample workbook.
    Attached Files Attached Files

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

    Re: Delete duplicate rows based on all columns

    Yeah, your workbook has some null values stored all over the place that completely invalidates the .SpecialCells(xlCellTypeLastCell) syntax. So we'll use a different approach.

    Also, your original request was to eliminate all duplicate rows, not just ONE of the duplicates, so I had to correct the logic on that, too.

    Use these macros in replacement:
    Please Login or Register  to view this content.

  15. #15
    Forum Contributor vijay2482's Avatar
    Join Date
    03-03-2009
    Location
    Paris,France
    MS-Off Ver
    Excel 2003
    Posts
    263

    Re: Delete duplicate rows based on all columns

    Thanks JBeaucaire,

    Will give it a try and get back soon.

    Vijay

  16. #16
    Forum Contributor vijay2482's Avatar
    Join Date
    03-03-2009
    Location
    Paris,France
    MS-Off Ver
    Excel 2003
    Posts
    263

    Re: Delete duplicate rows based on all columns

    Thanks a lot JBeaucaire.
    The macro did exactly what I wants.

    Thanks once again.
    Vijay

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

    Re: Delete duplicate rows based on all columns

    If that takes care of your need, be sure to EDIT your original post, click Go Advanced and mark the PREFIX box [SOLVED].

    (Also, use the blue "scales" icon in our posts to leave Reputation Feedback, it is appreciated. It is found across from the "time" in each of our posts.)

  18. #18
    Forum Contributor vijay2482's Avatar
    Join Date
    03-03-2009
    Location
    Paris,France
    MS-Off Ver
    Excel 2003
    Posts
    263

    Re: Delete duplicate rows based on all columns

    Its not my post.
    I have replied in this post.
    I'am not able to edit the prefix.

    Thanks,
    Vijay

  19. #19
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Delete duplicate rows based on all columns

    Quote Originally Posted by vijay2482
    Its not my post.
    I have replied in this post.
    I'am not able to edit the prefix.
    Which rather begs the question: why have you seen fit to ask your own questions in another member's thread ?

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