MrExcel Message Board


Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Jan 21st, 2009, 06:54 PM   #1
mgirvin
 
Join Date: Dec 2005
Location: Seattle, WA
Posts: 321
Default Concatenate Multiple Cells with Array Formula

Dear Smartest Excelers In The World,

If I have the following data set in cells A1:C9:

ID Comment First Unique
109876 Low Oil TRUE
109876 Checked On 12/12 FALSE
109877 Checked on 12/15 TRUE
109878 Correct Level TRUE
109877 Correct Level FALSE
109878 Perfect FALSE
109877 New FALSE
109878 Correct Level FALSE

In the First Unique column (column C) I have formulas. In C2 I have this formula (and then I copied it down):

=COUNTIF(A$2:A2,A2)=1

In cell E2, I have this formula to count unique occurrences:

=SUMPRODUCT(C2:C9*1)

In cell G2 I have this formula to extract unique records (entered with Ctrl + Shift + Enter):

=IF(ROWS(G$2:G2)<=E$2,INDEX(A$2:A$9,SMALL(IF(C$2:C$9=TRUE,ROW(C$2:C$9)-ROW(C$2)+1),ROWS(G$2:G2))),"")

Now, in cell H2 I would like a formula that would concatenate all the comments from the Comments column in the data set for each unique ID.

For example, For ID # 109876, I would like the formula to yield this text string in cell H2:

“Low Oil, Checked On 12/12”

Any ideas?
__________________
Sincerely, Mike Gel Girvin
mgirvin is offline   Reply With Quote
Old Jan 21st, 2009, 10:35 PM   #2
pgc01
MrExcel MVP
 
Join Date: Apr 2006
Posts: 7,256
Default Re: Concatenate Multiple Cells with Array Formula

Hi Mike

You cannot do it with a native formula. You have to use vba. You can write your own UDF or use someone else's.

For ex., if you use the MOREFUNC add-in, you can use the MCONCAT() UDF.

http://www.mrexcel.com/forum/showthread.php?t=126629
__________________
Kind regards
PGC

Always enclose code between the codetags [code] and [/code] (you can use the # button).
pgc01 is offline   Reply With Quote
Old Jan 21st, 2009, 10:45 PM   #3
mgirvin
 
Join Date: Dec 2005
Location: Seattle, WA
Posts: 321
Default Re: Concatenate Multiple Cells with Array Formula

Dear pgc01,

Thank you. That is good information.

Does the term "Native" mean one cell?
__________________
Sincerely, Mike Gel Girvin
mgirvin is offline   Reply With Quote
Old Jan 21st, 2009, 10:50 PM   #4
pgc01
MrExcel MVP
 
Join Date: Apr 2006
Posts: 7,256
Default Re: Concatenate Multiple Cells with Array Formula

Quote:
Does the term "Native" mean one cell?
No, it means the functions available in excel when you install it, like SUM(), VLOOKUP(), AVERAGE(), etc.

You can then add the functions you write, you call them UDFs (Used Defined Functions).

In this case you can write, for ex., a function that concatenates an array.
__________________
Kind regards
PGC

Always enclose code between the codetags [code] and [/code] (you can use the # button).
pgc01 is offline   Reply With Quote
Old Jan 21st, 2009, 10:59 PM   #5
jbeaucaire
 
jbeaucaire's Avatar
 
Join Date: May 2002
Location: Bakersfield, CA
Posts: 2,944
Default Re: Concatenate Multiple Cells with Array Formula

This function was posted here recently (Jindon?) and does what you want, I believe:
Code:
Function JoinAll(ByVal BaseValue, ByRef rng As Range, ByVal delim As String)
Dim a, i As Long
a = rng.Value
For i = 1 To UBound(a, 1)
    If a(i, 1) = BaseValue Then JoinAll = JoinAll & _
        IIf(JoinAll = "", "", delim) & a(i, 2)
Next
End Function
Used like:
=JoinAll(Cell,Range,Delimiter)

Here it is in action:
Sheet2

 ABCD
22109876Low Oil Low Oil, Checked On 12/12
23109876Checked On 12/12 Low Oil, Checked On 12/12
24109877Checked On 12/15 Checked On 12/15, Correct Level, New
25109878Correct Level Correct Level, Perfect, Correct Level
26109877Correct Level Checked On 12/15, Correct Level, New
27109878Perfect Correct Level, Perfect, Correct Level
28109877New Checked On 12/15, Correct Level, New
29109878Correct Level Correct Level, Perfect, Correct Level

Spreadsheet Formulas
CellFormula
D22=joinall(A22,$A$22:$B$29,", ")
D23=joinall(A23,$A$22:$B$29,", ")
D24=joinall(A24,$A$22:$B$29,", ")
D25=joinall(A25,$A$22:$B$29,", ")
D26=joinall(A26,$A$22:$B$29,", ")
D27=joinall(A27,$A$22:$B$29,", ")
D28=joinall(A28,$A$22:$B$29,", ")
D29=joinall(A29,$A$22:$B$29,", ")


Excel tables to the web >> Excel Jeanie HTML 4
__________________
"Actually I *am* a rocket scientist." -- JB
jbeaucaire is offline   Reply With Quote
Old Jan 22nd, 2009, 02:33 AM   #6
mgirvin
 
Join Date: Dec 2005
Location: Seattle, WA
Posts: 321
Default Re: Concatenate Multiple Cells with Array Formula

Dear jbeaucaire,

Wow!

That user defined function by Jindon is so efficient- so perfect for this task!

Blows away the formula approach that I was attempting.

Thanks!
__________________
Sincerely, Mike Gel Girvin
mgirvin is offline   Reply With Quote
Old Jan 22nd, 2009, 03:21 AM   #7
jbeaucaire
 
jbeaucaire's Avatar
 
Join Date: May 2002
Location: Bakersfield, CA
Posts: 2,944
Default Re: Concatenate Multiple Cells with Array Formula

Definitely. I looked up and confirmed the authorship and added it permanently to the code itself, appropriate, don't you think?

Code:
Function JoinAll(ByVal BaseValue, ByRef rng As Range, ByVal delim As String)
'code by Jindon, MrExcel.com MVP
Dim a, i As Long
a = rng.Value
For i = 1 To UBound(a, 1)
    If a(i, 1) = BaseValue Then JoinAll = JoinAll & _
        IIf(JoinAll = "", "", delim) & a(i, 2)
Next
End Function
Jindon
__________________
"Actually I *am* a rocket scientist." -- JB
jbeaucaire is offline   Reply With Quote
Old Jan 22nd, 2009, 03:25 AM   #8
mikerickson
MrExcel MVP
 
mikerickson's Avatar
 
Join Date: Jan 2007
Location: Davis CA
Posts: 8,456
Default Re: Concatenate Multiple Cells with Array Formula

The arguments of the UDF ConcatIf mirror those of SumIf, with the addtition of an optional Delimiter argument. For your situation the formula would be
=ConcatIf(A:A, 109876, B:B, ", ")
Code:
Function ConcatIf(ByVal compareRange As Range, ByVal xCriteria As Variant, Optional ByVal stringsRange As Range, _
                            Optional Delimiter As String, Optional NoDuplicates As Boolean) As String
    Dim i As Long, j As Long
    With compareRange.Parent
    Set compareRange = Application.Intersect(compareRange, Range(.UsedRange, .Range("a1")))
     End With
    If compareRange Is Nothing Then Exit Function
    If stringsRange Is Nothing Then Set stringsRange = compareRange
    Set stringsRange = compareRange.Offset(stringsRange.Row - compareRange.Row, _
                                                stringsRange.Column - compareRange.Column)
    
        For i = 1 To compareRange.Rows.Count
            For j = 1 To compareRange.Columns.Count
                If (Application.CountIf(compareRange.Cells(i, j), xCriteria) = 1) Then
                    If InStr(ConcatIf, Delimiter & CStr(stringsRange.Cells(i, j))) <> 0 Imp Not (NoDuplicates) Then
                        ConcatIf = ConcatIf & Delimiter & CStr(stringsRange.Cells(i, j))
                    End If
                End If
            Next j
        Next i
        ConcatIf = mid(ConcatIf, Len(Delimiter) + 1)
    End Function
mikerickson is online now   Reply With Quote
Old Jan 22nd, 2009, 03:55 AM   #9
Yogi Anand
MrExcel MVP
 
Join Date: Mar 2002
Location: Michigan USA
Posts: 11,448
Default Re: Concatenate Multiple Cells with Array Formula

The following solution uses MCONCAT function from MoreFunc Add-in as mentioned by PgC01 earlier ...

Excel Jeanie HTMLSheet7

*ABCDEF
2109876Low OilTRUE*109876Low Oil,Checked On 12/12
3109876Checked On 12/12FALSE***
4109877Checked on 12/15TRUE***
5109878Correct LevelTRUE***
6109877Correct LevelFALSE***
7109878PerfectFALSE***
8109877NewFALSE***
9109878Correct LevelFALSE***

Spreadsheet Formulas
CellFormula
F2{=SUBSTITUTE(MCONCAT(IF(A2:A9=E2,B2:B9),","),",FALSE","")}


Excel tables to the web >> Excel Jeanie HTML 4

__________________
Regards!

Yogi Anand, D.Eng, P.E.
Energy Efficient Building Network LLC
www.energyefficientbuild.com
Yogi Anand is offline   Reply With Quote
Old Jan 22nd, 2009, 04:55 AM   #10
mgirvin
 
Join Date: Dec 2005
Location: Seattle, WA
Posts: 321
Default Re: Concatenate Multiple Cells with Array Formula

Dear mikerickson and Yogi,

Wow, so many great solutions! Thanks!

And, yes, jbeaucaire, your extra code is appropriate. However, when I pasted mine, I put:

‘Jindon with help from jbeaucaire

You all are great!
__________________
Sincerely, Mike Gel Girvin
mgirvin is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT +1. The time now is 04:47 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
All contents Copyright 1998-2009 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes