[COLOR=black][FONT=Calibri]Function StringConcat(Sep As String, ParamArray Args()) As Variant[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]' StringConcat[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]' By Chip Pearson, chip@cpearson.com, www.cpearson.com[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]' www.cpearson.com/Excel/stringconcatenation.aspx[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]' This function concatenates all the elements in the Args array,[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]' delimited by the Sep character, into a single string. This function[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]' can be used in an array formula. There is a VBA imposed limit that[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]' a string in a passed in array (e.g., calling this function from[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]' an array formula in a worksheet cell) must be less than 256 characters.[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]' See the comments at STRING TOO LONG HANDLING for details.[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]Dim S As String[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]Dim N As Long[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]Dim M As Long[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]Dim R As Range[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]Dim NumDims As Long[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]Dim LB As Long[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]Dim IsArrayAlloc As Boolean[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]'''''''''''''''''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]' If no parameters were passed in, return[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]' vbNullString.[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]'''''''''''''''''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]If UBound(Args) - LBound(Args) + 1 = 0 Then[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] StringConcat = vbNullString[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] Exit Function[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]End If[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]For N = LBound(Args) To UBound(Args)[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ''''''''''''''''''''''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' Loop through the Args[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ''''''''''''''''''''''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] If IsObject(Args(N)) = True Then[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] '''''''''''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' OBJECT[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' If we have an object, ensure it[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' it a Range. The Range object[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' is the only type of object we'll[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' work with. Anything else causes[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' a #VALUE error.[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ''''''''''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] If TypeOf Args(N) Is Excel.Range Then[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] '''''''''''''''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' If it is a Range, loop through the[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' cells and create append the elements[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' to the string S.[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] '''''''''''''''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] For Each R In Args(N).Cells[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] If Len(R.Text) > 0 Then[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] S = S & R.Text & Sep[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] End If[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] Next R[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] Else[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] '''''''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' Unsupported object type. Return[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' a #VALUE error.[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] '''''''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] StringConcat = CVErr(xlErrValue)[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] Exit Function[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] End If[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ElseIf IsArray(Args(N)) = True Then[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] '''''''''''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' ARRAY[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' If Args(N) is an array, ensure it[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' is an allocated array.[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] '''''''''''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] IsArrayAlloc = (Not IsError(LBound(Args(N))) And _[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] (LBound(Args(N)) <= UBound(Args(N))))[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] If IsArrayAlloc = True Then[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ''''''''''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' The array is allocated. Determine[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' the number of dimensions of the[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' array.[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] '''''''''''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] NumDims = 1[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] On Error Resume Next[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] Err.Clear[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] NumDims = 1[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] Do Until Err.Number <> 0[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] LB = LBound(Args(N), NumDims)[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] If Err.Number = 0 Then[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] NumDims = NumDims + 1[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] Else[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] NumDims = NumDims - 1[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] End If[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] Loop[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] On Error GoTo 0[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] Err.Clear[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ''''''''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' The array must have either[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' one or two dimensions. Greater[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' that two caues a #VALUE error.[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ''''''''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] If NumDims > 2 Then[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] StringConcat = CVErr(xlErrValue)[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] Exit Function[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] End If[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] If NumDims = 1 Then[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] For M = LBound(Args(N)) To UBound(Args(N))[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] If Args(N)(M) <> vbNullString Then[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] S = S & Args(N)(M) & Sep[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] End If[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] Next M[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] Else[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ''''''''''''''''''''''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' STRING TOO LONG HANDLING[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' Here, the error handler must be set to either[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' On Error GoTo ContinueLoop[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' or[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' On Error GoTo ErrH[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' If you use ErrH, then any error, including[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' a string too long error, will cause the function[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' to return #VALUE and quit. If you use ContinueLoop,[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' the problematic value is ignored and not included[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' in the result, and the result is the concatenation[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' of all non-error values in the input. This code is[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' used in the case that an input string is longer than[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ' 255 characters.[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] ''''''''''''''''''''''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] On Error GoTo ContinueLoop[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] 'On Error GoTo ErrH[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] Err.Clear[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] For M = LBound(Args(N), 1) To UBound(Args(N), 1)[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] If Args(N)(M, 1) <> vbNullString Then[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] S = S & Args(N)(M, 1) & Sep[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] End If[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] Next M[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] Err.Clear[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] M = LBound(Args(N), 2)[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] If Err.Number = 0 Then[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] For M = LBound(Args(N), 2) To UBound(Args(N), 2)[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] If Args(N)(M, 2) <> vbNullString Then[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] S = S & Args(N)(M, 2) & Sep[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] End If[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] Next M[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] End If[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] On Error GoTo ErrH:[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] End If[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] Else[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] If Args(N) <> vbNullString Then[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] S = S & Args(N) & Sep[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] End If[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] End If[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] Else[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] On Error Resume Next[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] If Args(N) <> vbNullString Then[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] S = S & Args(N) & Sep[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] End If[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] On Error GoTo 0[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] End If[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]ContinueLoop:[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]Next N[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]'''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]' Remove the trailing Sep[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]'''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]If Len(Sep) > 0 Then[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] If Len(S) > 0 Then[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] S = Left(S, Len(S) - Len(Sep))[/FONT][/COLOR]
[COLOR=black][FONT=Calibri] End If[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]End If[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]StringConcat = S[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]'''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]' Success. Get out.[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]'''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]Exit Function[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]ErrH:[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]'''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]' Error. Return #VALUE[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]'''''''''''''''''''''''''''''[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]StringConcat = CVErr(xlErrValue)[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]End Function[/FONT][/COLOR]