vbastringoutlookinitialization

VBA: initializing a string array


New to VBA, and utterly frustrated. Just trying to simple initialize an array of strings.

Dim MyArray() As String

MyArray = Array("A", "B", "C")

Returns Run-time Error 13: type mismatch.

What am I missing?

Started with more complex strings, dumbed down to single letters but that obviously was not the problem.


Solution

  • Beginning is always frustrating but don't give up ^^

    Seems you are mixing variant with array.

    Using Array with a variant:

    Dimenter code here MyArray As Variant
    MyArray = Array("A", "B", "C")
    

    In case of Static Array:

    Dim MyArray(2) As String
    MyArray(0) = "A"
    MyArray(1) = "B"
    MyArray(2) = "C"
    

    Dynamic one:

    Dim MyArray() As String
    ReDim MyArray(2)
    MyArray(0) = "A"
    MyArray(1) = "B"
    MyArray(2) = "C"