Excel VBA Function to concatenate results separated by comma

gailb
3StarLounger
Posts: 254
Joined: 09 May 2020, 14:00

Excel VBA Function to concatenate results separated by comma

Post by gailb »

I would like to concatenate the values from range(A2:B13) if they meet the criteria in D2:D7. As you can see from this example, E7 should show nothing since there isn't any match for the 6 in D7. Here is what I'm currently using, but I'm open to any function that will do this in VBA.


Code: Select all

Option Explicit
Dim First As Boolean
Dim c As Variant

Function ConcatY(FinalRng As Variant) As Variant
First = True
ConcatY = "-"
For Each c In FinalRng
    If c <> "" Then
        If First = True Then
            ConcatY = c
            First = False
        Else
            ConcatY = ConcatY & ", " & c
        End If
    End If
Next c
End Function
You do not have the required permissions to view the files attached to this post.

User avatar
StuartR
Administrator
Posts: 12604
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Excel VBA Function to concatenate results separated by comma

Post by StuartR »

Have a look at the concatenateif function in this thread.
StuartR


gailb
3StarLounger
Posts: 254
Joined: 09 May 2020, 14:00

Re: Excel VBA Function to concatenate results separated by comma

Post by gailb »

Hi Stuart,

This works, but it still concatenates the last value even though the match is empty.

=ConcatenateIf($A$2:$A$13,D2,$B$2:$B$13," / ")

Jan / Feb
Mar / Apr
May / Jun
Jul / Aug
Sep / Oct
/

How can I get rid of that last slash?

User avatar
HansV
Administrator
Posts: 78465
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Excel VBA Function to concatenate results separated by comma

Post by HansV »

Here is a version that skips blanks:

Code: Select all

Function ConcatenateIf(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
        ConcatenateIf = CVErr(xlErrRef)
        Exit Function
    End If
    For i = 1 To CriteriaRange.Count
        If CriteriaRange.Cells(i).Value = Condition Then
            If ConcatenateRange.Cells(i).Value <> "" Then
                strResult = strResult & Separator & ConcatenateRange.Cells(i).Value
            End If
        End If
    Next i
    If strResult <> "" Then
        strResult = Mid(strResult, Len(Separator) + 1)
    End If
    ConcatenateIf = strResult
    Exit Function
ErrHandler:
    ConcatenateIf = CVErr(xlErrValue)
End Function
Use the same way as above.
Best wishes,
Hans

gailb
3StarLounger
Posts: 254
Joined: 09 May 2020, 14:00

Re: Excel VBA Function to concatenate results separated by comma

Post by gailb »

Perfect. Thanks Hans.

User avatar
p45cal
2StarLounger
Posts: 146
Joined: 11 Jun 2012, 20:37

Re: Excel VBA Function to concatenate results separated by comma

Post by p45cal »

What version of Excel do you have?
Perhaps builtin functions only with:
=TEXTJOIN(", ",TRUE,FILTER($B$2:$B$13,$A$2:$A$13=D2,""))

User avatar
p45cal
2StarLounger
Posts: 146
Joined: 11 Jun 2012, 20:37

Re: Excel VBA Function to concatenate results separated by comma

Post by p45cal »

I received a message there was a new post by HansV here but there's nothing?

User avatar
HansV
Administrator
Posts: 78465
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Excel VBA Function to concatenate results separated by comma

Post by HansV »

That is because Niranjanmeyda posted a new message in this thread that actually belonged in this one. I replied, but then moved the posts to the other thread. Sorry for the confusion!
Best wishes,
Hans