The use of A1&B1&C1&D1....etc is called String Concatenation and it does cause headaches, doesn't it? I have a collection of User Defined Functions for string concatenation. One of them has the ability to spot duplicates and suppress them.
Unfortunately, it's hard to test on your sheet, so here's a simple explanation and sample sheet. First, here's the code for it, and it goes in a standard module:
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
' code base by Mike Rickson, MrExcel MVP
' used as exactly like SUMIF() with two additional parameters
' of delimiter and "no duplicates" as TRUE/FALSE if concatenated values
' might include duplicates ex. =ConcatIf($A$1:$A$10,C1,$B$1:$B$10,", ",True)
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
It's used just like a SUMIF() formula, but it has two extra parameters.
=ConCatIf(A1:A10,"<>"&BB1,A1:A10,", ",TRUE)
First parameter: Range to evaluate
Second param: comparison string. (I chose an empty cell, so this selects all cells that aren't empty in the range)
Third param: Range to return values from (I used the same range as first param, but you don't have to)
Fourth param: The delimiting string for the concatenation, I'm using a comma/space in this example
Fifth param: (optional) TRUE means eliminate duplicated values, FALSE (or omitted) means string all values
This should give you enough info to apply this UDF to your sheet and use it place of that formula of your own...your sample formula of:
=N23&O23&P23&Q23&R23&S23&T23&U23&V23&W23
...would become:
=ConcatIf(N23:W23,"<>"&BB23,N23:W23," ",TRUE)
Bookmarks