+ Reply to Thread
Results 1 to 16 of 16

Delete rows that have duplicate data in certain columns

  1. #1
    Registered User
    Join Date
    04-19-2013
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    24

    Delete rows that have duplicate data in certain columns

    Hey everyone,

    I am currently working in excel2007 with a pretty big file of over 13,000 rows and 27 columns. A lot of these rows need to be deleted, because, for the most part, they are duplicates.

    I am looking to incorporate a macro that will look at 4 or 5 columns in each row and, if ALL of these columns are identical for a group of rows, delete all but one of these rows. the macro will need to be able to search all the way to the bottom of the data and delete rows where these criteria are met.

    Additionally, one of the columns that NEVER is duplicated contains an individuals name, and for each row that i delete i want to consolidate the persons names into a cell in the row that i keep to look like: Bob | Allison | Matt ....etc

    I hope i explained it well enough. I cannot provide much of a sample since it is sensitive information.

    I appreciate your help!
    Last edited by aarodn; 04-19-2013 at 07:48 PM.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,475

    Re: Delete rows that have duplicate data in certain columns

    Attach a sample workbook. Make sure there is just enough data to make it clear what is needed. Include BEFORE/AFTER sheets if needed to show the process you're trying to complete or automate. Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

  3. #3
    Registered User
    Join Date
    04-19-2013
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Delete rows that have duplicate data in certain columns

    I have attached a sample of what I am talking about.

    In this example, I have employee information. I want to group names together that meet criteria, which is, they all work at the same department, company, and state. I have highlighted in yellow the criteria.

    When all three criteria are identical, i want the macro to group all names together and delete duplicates.

    Please let me know if the attachment is not visible or if more info is needed.


    Thanks!!
    Attached Files Attached Files

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: Delete rows that have duplicate data in certain columns

    Please Login or Register  to view this content.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    04-19-2013
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Delete rows that have duplicate data in certain columns

    beautiful. thank you this is a huge help.

    if i wanted to use this macro with additional criteria columns i would add them to the line

    myKey = Array(2, 4, 5, 7...etc

    is that correct? would i have to modify the code at all elsewhere?

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: Delete rows that have duplicate data in certain columns

    That's it. No need to change any part of the code.

  7. #7
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Delete rows that have duplicate data in certain columns

    @ jindon,

    Excellent !

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: Delete rows that have duplicate data in certain columns

    Thanks....

  9. #9
    Registered User
    Join Date
    04-19-2013
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Delete rows that have duplicate data in certain columns

    jindon,

    i am trying to adapt the code to my workbook, and i get a run-time error '9': subscript out of range. when i click 'debug', it highlights the second line of code that starts with a = sheets...


    i have a few questions remaining:

    1)could you explain the details of this line (a = sheets) so i could modify it correctly?
    2)what part of code is responsible for pointing to the 'names' column? I need to modify what column has the names.
    3)what is the purpose of Option Explicit?

    Thank you

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: Delete rows that have duplicate data in certain columns

    1) You need to replace "before" with actual sheet name.
    It stores the data in variable "a" to speed up the process.
    Please Login or Register  to view this content.
    2) Name column should be included in an array "myKey" to make unique key.

    3) It is always better to declare it. It forces to declare all the variable used in the module.

  11. #11
    Registered User
    Join Date
    04-19-2013
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Delete rows that have duplicate data in certain columns

    where in the code is the name column referenced?

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: Delete rows that have duplicate data in certain columns

    Please Login or Register  to view this content.
    First column in the data range.

  13. #13
    Registered User
    Join Date
    04-19-2013
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Delete rows that have duplicate data in certain columns

    okay last question, i promise!

    instead of inserting the finished product into a new sheet, i want the macro to replace over the existing data, essentially shrinking down the range of data. I assume the last 'with' argument in the current code will be removed, but what must be added to ensure the consolidated version is pasted over the 'before' data??

    Thank you very, very, very much!!!!

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: Delete rows that have duplicate data in certain columns

    See the difference.
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    04-19-2013
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Delete rows that have duplicate data in certain columns

    jindon,

    i cannot make the code work in my real workbook. Please see the attachment, which contains only the headings of my real workbook. columns D,R,&S need to be compared and the names in col T need to be consolidated for duplicate rows.
    Attached Files Attached Files

  16. #16
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: Delete rows that have duplicate data in certain columns

    aarodn

    You should remember, one of the most important issue to write the code is to get the correct data range, otherwise the code will never work.

    Your first sheet has Column A blank (I don't like it though), so I wrote
    Please Login or Register  to view this content.
    This gets the range of rectangle area includes "B1" until the boundary of blank row/column.
    So the relative col reference within the data area will shift by 1 from the actual sheet col reference.

    Now you have data from A1, which I prefer, so now the col reference is identical to the sheet.
    Now Col.A = 1, Col.B = 2 etc. Was Col.B = 1, Col.C = 2 etc.

+ 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