+ Reply to Thread
Results 1 to 15 of 15

VBA to Delete Duplicate Rows and Concatenate a String

  1. #1
    Registered User
    Join Date
    10-10-2009
    Location
    New York, New York
    MS-Off Ver
    Excel 2003
    Posts
    10

    Post VBA to Delete Duplicate Rows and Concatenate a String

    Hello,
    I am trying to develop a macro that will concatenate strings and separate them with commas in one cell and delete duplicate

    Example:

    ID Name Region Area
    23 Red US East
    42 Blue Aus NA
    324 Green US North
    243 Purple US All
    243 Purple Can All
    32 Black US West
    23 Red Can East

    What I would like:

    ID Name Region Area
    23 Red US, Can East
    42 Blue Aus NA
    324 Green US North
    243 Purple US, Can All
    32 Black US West

    The issue I am running into is that I can't set a macro to concatenate easily because there are columns before and after the column I'd like to concatenate. Any assistance in this matter would be greatly appreciated. Thanks!

    I am also attaching an excel example of this.

    Sample1.xlsx

  2. #2
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Re: VBA to Delete Duplicate Rows and Concatenate a String

    What columns do you want to concatenate?

    Please Login or Register  to view this content.
    This above concatenates columns A and B together with a comma in between. The result goes into column D. This won't solve your duplicate problem at the moment.

  3. #3
    Registered User
    Join Date
    10-10-2009
    Location
    New York, New York
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: VBA to Delete Duplicate Rows and Concatenate a String

    In this case, it would be Column C, the Region column . . .

    Essentially, I would like to look at Column A and for any duplicates values, concatenate Column C with a comma for the duplicates and then delete the duplicate rows.

    The code JBeauclaire posted is similar to what I want I think, except that there are columns before and afterwards:

    http://www.excelforum.com/excel-prog...uplicates.html

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

    Re: VBA to Delete Duplicate Rows and Concatenate a String

    How about
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    10-10-2009
    Location
    New York, New York
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: VBA to Delete Duplicate Rows and Concatenate a String

    The previous script didn't really do anything when I . . . perhaps I'm missing something.

    I think I can easily write a script to delete duplicate rows of data. The problem is that ALL the data in the row, other than the region data, is the same. I'd like to combine the region data in that case so that they are in one cell and separated by a comma.

    I think I can also write script to combine data into a single cell being separated by a comma, however, I'm running into problems because the regions column has data in both columns to the left and right of it.

    If anyone could provide any insight that would be appreciated. Thanks!

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

    Re: VBA to Delete Duplicate Rows and Concatenate a String

    I don't understand what you are talking about.

    It is working.

    Sample1WithCode.xlsm

  7. #7
    Registered User
    Join Date
    10-10-2009
    Location
    New York, New York
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: VBA to Delete Duplicate Rows and Concatenate a String

    All right . . . I tried to put it into a command button and must have messed up. I actually wanted it to replace the information on Sheet 1 so I just changed the code to paste onto Sheet 1. Thank you for all the help

  8. #8
    Registered User
    Join Date
    10-10-2009
    Location
    New York, New York
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: VBA to Delete Duplicate Rows and Concatenate a String

    Thank you so much for the help!

    I had one quick question. Would it be possible to scan for duplicates and only concatenate the Region data that is different? i.e if I have:

    ID Name Region Area
    23 Red US East
    23 Red US East

    I would just delete the extra row, but if I had:

    ID Name Region Area
    23 Red US East
    23 Red Can East

    It would become:

    ID Name Region Area
    23 Red US, Can East

    Thank you for the help once again!

  9. #9
    Registered User
    Join Date
    10-10-2009
    Location
    New York, New York
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: VBA to Delete Duplicate Rows and Concatenate a String

    Sorry to ask again but I just had a request and I'm not sure if it was possible but, is it possible to add in an input box for the column that I want to concatenate? ie, if I want to concatenate column 3 instead of 4, is it possible to put a pop up box that prompts for what column to combine?

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

    Re: VBA to Delete Duplicate Rows and Concatenate a String

    Your last post is not clear to me.

    What if the data like


    ID Name Region Area
    23 Red US East
    23 Red Can East

    And you select "Area" to be concatenated.

    Need the detailed data with the desired result in Excel

  11. #11
    Registered User
    Join Date
    10-10-2009
    Location
    New York, New York
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: VBA to Delete Duplicate Rows and Concatenate a String

    Than you for the response.

    I had two questions: a) Currently, the code is concatenating everything in the the indicated column. I was wondering if there was a method to only concatenate if the values in the column are different.

    b) if I'd like to have the user select what column to concatenate, instead of having a set column coded in, is it possible to do it via a input box?

    I've attached the spreadsheet and thank you once again!

    Sample1WithCode.xlsm
    Attached Files Attached Files

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

    Re: VBA to Delete Duplicate Rows and Concatenate a String

    You are not answering to my last question.

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    10-10-2009
    Location
    New York, New York
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: VBA to Delete Duplicate Rows and Concatenate a String

    Sorry about that . . .

    If Area is selected, to be concatenated, I'd expect it not return just:

    ID Name Region Area
    23 Red US East

    since there is only East in Area. Ideally, I would like it to scan through the row and if there are values in a column that are not the same, to concatenate, but I don't think that would be easy. In reality, the only column I expect to have data that is different is the Region, but the reason I am asking for the ability to select different columns is because sometimes in different worksheets, the Region metric will be located in different columns (ie, instead of 3, it may be in 6).

    Sorry for the confusion.

  14. #14
    Registered User
    Join Date
    10-10-2009
    Location
    New York, New York
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: VBA to Delete Duplicate Rows and Concatenate a String

    Works great, thanks for all the help!

  15. #15
    Registered User
    Join Date
    10-10-2009
    Location
    New York, New York
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: VBA to Delete Duplicate Rows and Concatenate a String

    Sorry for the confusion again . . . I realized that the current code after running deletes out some of the cells where I have formulas. Is there a solution to this?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

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