1

I usually use variants when I need an array:

Sub test()   'runs fine
    Dim s
    s = Array("a", "b", "c")
    Debug.Print s(1)
End Sub

Now I want performance for a specific case, so I thought I could save a few millisec by using a String array. How do I do that?

My try:

Sub test2()
    Dim s() As String
    s = Array("a", "b", "c")     'run time error 13: type mismatch
    Debug.Print s(1)
End Sub 

Thanks.

2
  • 2
    Array() function returns a Variant containing an array. Commented Dec 20, 2017 at 11:56
  • Did you see this question? Commented Dec 20, 2017 at 12:03

1 Answer 1

3

Try this:

Sub test2()
    Dim s1 As Variant
    Dim s2() As String
    s1 = Array("a", "b", "c")
    s2 = Split("a,b,c", ",")

    Debug.Print VarType(s1) & " Array of variants"
    Debug.Print VarType(s2) & " Array of strings"
End Sub

Output:

8204 Array of variant
8200 Array of string
Sign up to request clarification or add additional context in comments.

2 Comments

Excellent ! That exactly covers my case, and makes the array easier to code as a bonus. All I have to see now is if it's any faster :-)
Strangely, the 'pure string' version seems slower.

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.