Closed Thread
Results 1 to 18 of 18

Concatenate cells and delete duplicates

  1. #1
    Registered User
    Join Date
    03-31-2009
    Location
    Spokane, WA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Concatenate cells and delete duplicates

    I have an excel 2007 spreadsheet that lists
    items on multiple rows for the same customer.

    Each sheet will list anywhere from 25 to 100 individual customers.

    I need to identify the duplicates, concatenate
    the "Items" to a single cell on the first row, and
    delete the duplicate rows. eg:

    Cust #| Item
    1 | A
    1 | B
    1 | C
    2 | B
    2 | E
    3 | A
    3 | C
    3 | E

    The result should look like:

    Cust# | New Item
    1 | A, B, C
    2 | B, E
    3 | A, C, E

    Can this be done? Thanks!
    Last edited by VBA Noob; 04-03-2009 at 01:52 AM.

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

    Re: Concatenate cells and delete duplicates

    First, you need to add a little "string concatentation" utility to your sheet to make this easier. And the macro that follows will do the work you wanted. It assumes there's nothing in columns C and D. If that won't work, let me know and we'll insert some blank columns first.

    Press Alt-F11 to open the VBEditor
    Click Insert > Module
    Paste in all this code (function and macro)
    Please Login or Register  to view this content.
    Press Alt-F11 to close the editor
    Save your sheet.

    Press Alt-F8 and run the "consolidate" macro.
    Last edited by JBeaucaire; 04-01-2009 at 07:40 PM. Reason: Updated the CONSOLIDATE macro a little
    _________________
    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!)

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Concatenate cells and delete duplicates

    Hello plente,

    Welcome to the Forum!

    This macro doesn't care if the data is sorted or not. It only clears the cells involved and not the entire row. You can change the starting cell if you need to. It is marked in the code in reed.
    Please Login or Register  to view this content.
    Adding the Macro
    1. Copy the macro above pressing the keys CTRL+C
    2. Open your workbook
    3. Press the keys ALT+F11 to open the Visual Basic Editor
    4. Press the keys ALT+I to activate the Insert menu
    5. Press M to insert a Standard Module
    6. Paste the code by pressing the keys CTRL+V
    7. Make any custom changes to the macro if needed at this time.
    8. Save the Macro by pressing the keys CTRL+S
    9. Press the keys ALT+Q to exit the Editor, and return to Excel.

    To Run the Macro...
    To run the macro from Excel, open the workbook, and press ALT+F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    03-31-2009
    Location
    Spokane, WA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Concatenate cells and delete duplicates

    Thanks for the replies! I guess a tad more info was needed. The "Cust#" range is indeed column A, but the "Item" range on the worksheet is actually AC, and there are indeed several more columns to the right of AC. In fact, the entire spreadsheet is from A - AH. The actual rows of customers are anywhere from a single row to as many as seven or eight, each identical except for the "Item" column.

    *Update* JBeaucaire; I have been trying to make your macro work. I tried running it against a simple, two column worsksheet. It eliminates the dups and copies the cust#s to row C as expected, but all I get in row D following each number is "#NAME?"

    Leith Ross; Your macro works perfectly against the two column worksheet, but how can I make it find and concatenate the correct column?

    Thanks again
    JR Jones
    Last edited by plente; 04-02-2009 at 05:39 PM. Reason: Additional info

  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: Concatenate cells and delete duplicates

    that's because you didn't install the UDF as well.

    If you post up a sample of your actual workbook, I can tweak it quickly and get it working.

  6. #6
    Registered User
    Join Date
    03-31-2009
    Location
    Spokane, WA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Concatenate cells and delete duplicates

    Thanks so much. Here's a copy with some data obfuscated. The relevant rows are 'C' Customer_ID, and 'AC' Recommendation.

    JR Jones
    Attached Files Attached Files

  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: Concatenate cells and delete duplicates

    Sorry if I'm dense, so let's start over. You want to concatenate all the comments in column AC that match the code in column C? Is that correct?

    Where is this concatenation supposed to appear when it's done?

    And what are all those concatenated duplicate dates in column B?

  8. #8
    Registered User
    Join Date
    03-31-2009
    Location
    Spokane, WA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Concatenate cells and delete duplicates

    My fault, sorry. I had run the other macro against that sheet. I didn't even notice that I had saved the result 'till just now, hence the concatenated dates in B and all the empty cells in A&B.

    Here's the deal; they want me to create a mail-mergeable spreadsheet to forward the service recommendations to the customer. The software that generates this list puts each recommendation on a new row (see column AC) Column C lists each customer's unique number, so it's ideal for identifying & later deleting the duplicate rows. If all the service recommendations are first copied from the duplicate rows, then concatenated into a single cell in column AC, or even in a new cell, it will all end up in one merge field in the final document.

    Does that makes things a bit clearer? I truly appreciate your efforts on this.

    JR Jones

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

    Re: Concatenate cells and delete duplicates

    So, all the values in AC matching invoice in C, concatenated to another spot, then copied BACK over column AC, then duplicates removed?

    Perhaps this is completed onto a second sheet to leave the original data alone? If so, what cells would you really want in that final report?

  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: Concatenate cells and delete duplicates

    Here's your sheet back with the data being created for you over in columns AP & AQ. Run the "Consolidate" macro.

    Here's what that macro looks like. It does still use the ConcatIf UDF as well, so both pieces of code need to be on your sheet.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by JBeaucaire; 04-06-2009 at 01:58 PM.

  11. #11
    Registered User
    Join Date
    03-31-2009
    Location
    Spokane, WA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Concatenate cells and delete duplicates

    Thanks again, JBeaucaire. I can't believe the time & effort that you've put into this!

    I'm really feeling like a dummy here. I was out of town for most of the weekend, but I was playing with this and trying to make it work Friday evening & most of yesterday. Man, I can usually look at a macro and muddle out what the relevant operations are, but not this one How does it know to get values from column AC?

    It runs OK on the 'trialsheet' that I attached, but when I try to run it against the actual speadsheet, it fails. Just gives the "NAME" error in column AQ. Googling around for info on UDFs is telling me that maybe they aren't enabled on my spreadsheet? I even saved the 'TrialSheet' file as macro-enabled to the XLStart folder, and the spreadsheet is saved in a trusted location. I finally copied & pasted the data from the real sheet into the 'TrialSheet" workbook, and it works just fine! What am I missing to enable UDFs?

    As far as a second sheet, That's not necessary if the duplicates can be just deleted. The mailer is going to need data from D,E,F.G,H,I,O,P,Q,T,U, & of course, AC.

    Here's a fresh copy of the sheet, in case I messed something up on the other one as well.

    I'm gonna owe ya big time if this can be made to work.
    Last edited by plente; 04-06-2009 at 02:38 PM.

  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: Concatenate cells and delete duplicates

    I don't use Excel 2007, so I can't advise you on that. You should ONLY be working with .xlsm files, I know THAT much. .xlsx don't use macros at all, it's a secure format.

    There was a wee typo in the macro, though it would still work. Here's your sheet again WITH the macro and UDF installed. You can actually save those module out to the hard drive as .bas files from inside the VBEditor (File > Export File while the module is highlighted). That could speed up installing it into future sheets.

    This works. Not sure what's left for you here.
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    03-31-2009
    Location
    Spokane, WA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Concatenate cells and delete duplicates

    Whatever you did worked! Is there a piece of code I can add to make it delete the duplicate rows?

    Thanks for the info on xlsx vs xlsm. Hadn't run into that as everything round here usually gets saved as xls.

  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: Concatenate cells and delete duplicates

    Delete rows, sure? But delete them where? Is this a whole new thing or something more you want to happen within the existing macro? The list I'm making doesn't have any duplicates....

  15. #15
    Registered User
    Join Date
    03-31-2009
    Location
    Spokane, WA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Thumbs up Re: Concatenate cells and delete duplicates

    The extra rows that contained the values in AC that was copied to AQ. Actually, I just figured that one out for myself. I just added :

    Sub Delete_Duplicates()
    '
    ' Delete_Duplicates Macro
    ' Gets rid of extra rows in Recommended Services
    '

    '
    Columns("A:AJ").Select
    Range("AJ1").Activate
    ActiveSheet.Range("$A$1:$AJ$420").RemoveDuplicates Columns:=3, Header:= _
    xlYes
    Range("AK2").Select
    End Sub

    And it got rid of the extra data and everything lines up perfectly with the appropriate cell in AQ!

    JBeaucaire, thank you so much for your patience and expertise. I hope I can one day repay you.

    I'd still like to know which line in your code identifies column AJ as a source

    Thanks;
    JR Jones
    Last edited by plente; 04-06-2009 at 05:59 PM. Reason: none, really

  16. #16
    Registered User
    Join Date
    03-31-2009
    Location
    Spokane, WA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Concatenate cells and delete duplicates

    Quote Originally Posted by JBeaucaire View Post

    This works. Not sure what's left for you here.
    JBeaucaire; would you be so kind as to delete the attachments in your post?

    Thanks again
    JR Jones

  17. #17
    Registered User
    Join Date
    02-17-2011
    Location
    Euless, Texas
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: Concatenate cells and delete duplicates

    I have this same issue, but the code provided doesn't seem to work with Excel 2007. It sucessfully removes duplicates in column A, but doesn't compile the column B like the example show. Is there something I should change for the newer excel version?

    David Allen


    Quote Originally Posted by JBeaucaire View Post
    First, you need to add a little "string concatentation" utility to your sheet to make this easier. And the macro that follows will do the work you wanted. It assumes there's nothing in columns C and D. If that won't work, let me know and we'll insert some blank columns first.

    Press Alt-F11 to open the VBEditor
    Click Insert > Module
    Paste in all this code (function and macro)
    Please Login or Register  to view this content.
    Press Alt-F11 to close the editor
    Save your sheet.

    Press Alt-F8 and run the "consolidate" macro.

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

    Re: Concatenate cells and delete duplicates

    Start your own thread. Include your sample workbook and this macro if desired. PM me a link to the new thread if you wish.

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