1

VBA Beginner here.

I am trying to pass an array of strings from a subroutine to a function which will then modify each string in the array. However I get the "Type:array or user-defined type expected" error message.

I have tried redefining different data types for the array so it is aligned with the data type entered in the function but to no avail.

Hope you can help! THank you so much!

Below is the dummy code:

Sub text()
    Dim haha() As Variant

    haha = Array("Tom", "Mary", "Adam")
    testing (haha())
    MsgBox Join(haha, " ")

End Sub

Function testing(ByRef check() As String) As String()
    Dim track As Long

    For track = LBound(check) To UBound(check)
        check(track) = check(track) & " OMG"
    Next
End Function
1
  • Use Function testing(ByRef check As Variant) As String() and testing haha to call it. Commented Jul 28, 2015 at 9:39

3 Answers 3

1

In orignial code, a string is not the same variant (I believe they both would need to be variant? someone can verify), you dont need the brackets after testing, only need brackets if you are setting to another value e.g.

haha2 = testing(haha())

Below code should be ok

Sub text()
Dim haha()
haha = Array("Tom", "Mary", "Adam")
testing haha()
MsgBox Join(haha, " ")

End Sub

Function testing(ByRef check()) As String
Dim track As Long
For track = LBound(check) To UBound(check)
    check(track) = check(track) & " OMG"
Next
End Function
Sign up to request clarification or add additional context in comments.

Comments

1

You have a few errors in your code:

  • There are two ways of invoking methods:

    1) with Call keyword - in this case you must give all the parameters in brackets:

    Call testing(haha)

    2) without Call keyword - in this case you just give your parameters after the name of function:

    testing haha

    In your code you combined both of them and this is syntax error.

  • If you pass an array as a parameter to function you don't need to put brackets like that: testing (haha()).

    The proper syntax is:

    testing(haha)

  • Function testing requires as a parameter an array of String type, you cannot pass object of other type instead since it causes compile error Type mismatch. Currently you are trying to pass variable haha which is of Variant type.

  • You can change the type of haha variable to array of strings (to avoid the error described above):

    Dim haha() As String
    

    However, in this case you cannot assign the value of function Array to it, since the result of this function is of Variant type. You would have to replace this code:

    haha = Array("Tom", "Mary", "Adam")
    

    with this:

    ReDim haha(1 To 3)
    haha(1) = "Tom"
    haha(2) = "Mary"
    haha(3) = "Adam"
    

Comments

0

A couple of suggestions to improve your code:

Dim haha() As String

You define the type of the entry in the array, not the array itself. Use the way mentioned by mielk to fill the array.

Function Testing(byref check as variant) As String

This will avoid problems with undefined variables. Not clear why you feel that the function should return a string though. Maybe even convert to a Sub instead.

Comments

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.