1

I am still new to VBA. here is the code:

Sub Macro1()
Dim NumberSeq() As Integer = {1,2,3,4}
End Sub

It's just the initialization of an array and I saw it work for others. But I always get the warning window: Compile error: Expected: end of statement with highlight on the equal mark"="

Don't know what happened here. Please advice, thank you a lot!!

2
  • 3
    VBA doesn't support declaration and assignment in one line. Commented Jun 17, 2013 at 21:51
  • Closest you could get might be Dim a(): a = [{1,2,3}] - but note that's a variant array not an integer one Commented Jun 17, 2013 at 21:59

1 Answer 1

2
Sub macro1()
Dim NumberSeq() As Variant

NumberSeq = Array(1, 2, 3, 4) 'an allocated array with length 1 to 4

'check output through print
For i = LBound(NumberSeq) To UBound(NumberSeq)
    msg = msg & NumberSeq(i) & vbNewLine
Next i
MsgBox (msg)

End Sub

For implicitly sized 2 dimensional arrays use Array(row start To row end, col start to col end).

Here is a good resource.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much for both of your help! It works! It seems without the "As Variant" also works. And thank you for sharing the website resource:)
No problem! there is a lot of great information there. If I answered your question please click the check mark next to my answer, thanks!

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.