1

I wish to use an Excel array function to generate an array of strings and then pass this to a user defined function to strip blanks and concatenate the strings separated by a ",".

I have a function that does this when called from a VBA macro. When I try to use it as a user defined function, e.g. =ConcStr({"A","B","C"}), I get a #Value! error.

Function is below:

Sub StrTest()
    Dim StaticArray(1 To 3) As String
    Dim Result As String
    StaticArray(1) = "A"
    StaticArray(2) = "B"
    StaticArray(3) = "C"
    Result = ConcStr(Arr:=StaticArray)
    MsgBox Result
End Sub

Function ConcStr(Arr() As String) As String
    MsgBox "started"
    Dim N As Long
    Dim Total As String
    For N = LBound(Arr) To UBound(Arr)
        MsgBox Arr(N)
        Total = Total & "," & Arr(N)
    Next N
    ConcStr = Total
End Function
1
  • IIRC, you cannot interchange fixed size arrays and dynamic arrays. Commented Aug 26, 2015 at 1:15

3 Answers 3

1

If you rewrite your UDF to accept a Variant instead, it should work. Also, you can just use the Join function to accomplish what you need:

Function ConcStr(arr As Variant) As String
    ConcStr = Join(arr, ",")
End Function
Sign up to request clarification or add additional context in comments.

Comments

0

Declare, dim, assign and pass the array over as a variant.

Sub StrTest()
    Dim StaticArray As Variant, Result As String

    ReDim StaticArray(1 To 3)

    StaticArray(1) = "A"
    StaticArray(2) = "B"
    StaticArray(3) = "C"

    Result = ConcStr(Arr:=StaticArray)
    MsgBox Result
    Result = ConcStr2(Arr:=StaticArray)
    MsgBox Result

End Sub

Function ConcStr(Arr As Variant) As String
    MsgBox "started"

    Dim N As Long, Total As String

    For N = LBound(Arr) To UBound(Arr)
        MsgBox Arr(N)
        Total = Total & "," & Arr(N)
    Next N

    ConcStr = Mid(Total, 2) 'Mid to get rid of the first comma

End Function

Function ConcStr2(Arr As Variant) As String
    'could just be like this,
    ConcStr2 = Join(Arr, ",")
End Function

I've added an alternative Join Function version to simplfy things and modified your function with the Mid function to remove the leading comma.

1 Comment

Thank you all, much appreciated
0

I was able to get what you want with:

Public Function ConcatString(ByVal arr As Variant) As String
    ConcatString = vbNullString
    Dim i As Long, n As Long, z as Long
    z = LBound(arr) : n = UBound(arr)
    For i = z To n
        ConcatString = ConcatString + arr(i)
    Next i
End Function

use

3 Comments

A UDF with array argument {"A","B","C"} does not pass as an static array in VBA. Variant is what should be used instead.
What if the arr being passed in was zero-based, e.g. with an LBound(arr) of 0? Your For... Next is going to skip the first array element.
Well yes. So I fixed the post. I was assuming it was going to be used as an UDF only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.