![]() |
|
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Join Date: Dec 2005
Location: Seattle, WA
Posts: 321
|
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 |
|
|
|
|
|
#2 |
|
MrExcel MVP
Join Date: Apr 2006
Posts: 7,256
|
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). |
|
|
|
|
|
#3 |
|
Join Date: Dec 2005
Location: Seattle, WA
Posts: 321
|
Dear pgc01,
Thank you. That is good information. Does the term "Native" mean one cell?
__________________
Sincerely, Mike Gel Girvin |
|
|
|
|
|
#4 | |
|
MrExcel MVP
Join Date: Apr 2006
Posts: 7,256
|
Quote:
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). |
|
|
|
|
|
|
#5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Join Date: May 2002
Location: Bakersfield, CA
Posts: 2,944
|
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
=JoinAll(Cell,Range,Delimiter) Here it is in action: Sheet2
Excel tables to the web >> Excel Jeanie HTML 4
__________________
"Actually I *am* a rocket scientist." -- JB |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|
#6 |
|
Join Date: Dec 2005
Location: Seattle, WA
Posts: 321
|
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 |
|
|
|
|
|
#7 |
|
Join Date: May 2002
Location: Bakersfield, CA
Posts: 2,944
|
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
__________________
"Actually I *am* a rocket scientist." -- JB |
|
|
|
|
|
#8 |
|
MrExcel MVP
Join Date: Jan 2007
Location: Davis CA
Posts: 8,456
|
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
|
|
|
|
|
|
#9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
MrExcel MVP
Join Date: Mar 2002
Location: Michigan USA
Posts: 11,448
|
The following solution uses MCONCAT function from MoreFunc Add-in as mentioned by PgC01 earlier ...
Excel tables to the web >> Excel Jeanie HTML 4
__________________
Regards! Yogi Anand, D.Eng, P.E. Energy Efficient Building Network LLC www.energyefficientbuild.com |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|
#10 |
|
Join Date: Dec 2005
Location: Seattle, WA
Posts: 321
|
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 |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|