ConcatenateIfs Function

charlee63

Board Regular
Joined
Jan 7, 2010
Messages
146
Here is my VGA
Function ConcatenateIfs(CriteriaRange As Range, Condition As Variant, _
ConcatenateRange As Range, Optional Separator As String = ",") As Variant
Dim i As Long
Dim strResult As String
On Error GoTo ErrHandler
If CriteriaRange.Count <> ConcatenateRange.Count Then
ConcatenateIfs = CVErr(xlErrRef)
Exit Function
End If
For i = 1 To CriteriaRange.Count
If CriteriaRange.Cells(i).Value = Condition Then
strResult = strResult & Separator & ConcatenateRange.Cells(i).Value
End If
Next i
If strResult <> "" Then
strResult = Mid(strResult, Len(Separator) + 1)
End If
ConcatenateIfs = strResult
Exit Function
ErrHandler:
ConcatenateIfs = CVErr(xlErrValue)
End Function

Result[TABLE="width: 84"]
<tbody>[TR]
[TD]69444,69444,69444,69444,69444,69444,69444,69444,69444,69707,69707

Formula
=ConcatenateIfs('Compare - NYD'!$A$2:$A$500,A17,'Compare - NYD'!$B$2:$B$500,", ")[/TD]
[/TR]
</tbody><colgroup><col></colgroup>[/TABLE]

How do I get Dups out?
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try:

Code:
Function ConcatenateIfs(CriteriaRange As Range, Condition As Variant, _
ConcatenateRange As Range, Optional Separator As String = ",", Optional NoDups As Boolean = False) As Variant
Dim i As Long, strResult As String, MyDict As Object

    On Error GoTo ErrHandler
    If CriteriaRange.Count <> ConcatenateRange.Count Then
        ConcatenateIfs = CVErr(xlErrRef)
        Exit Function
    End If
    Set MyDict = CreateObject("Scripting.Dictionary")
        
    For i = 1 To CriteriaRange.Count
        If CriteriaRange.Cells(i).Value = Condition Then
            strResult = strResult & Separator & ConcatenateRange.Cells(i).Value
            MyDict(CStr(ConcatenateRange.Cells(i).Value)) = 1
        End If
    Next i
    
    ConcatenateIfs = IIf(NoDups, Join(MyDict.keys, Separator), Mid(strResult, Len(Separator) + 1))
    
    Exit Function
    
ErrHandler:
    ConcatenateIfs = CVErr(xlErrValue)

End Function
And call it with an extra (optional) parameter like this:

=ConcatenateIfs('Compare - NYD'!$A$2:$A$500,A17,'Compare - NYD'!$B$2:$B$500,", ",TRUE)

to remove duplicates.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top