Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing Array in Excel VBA - getting error

Tags:

excel

vba

In the microsoft site:

http://msdn.microsoft.com/en-us/library/vstudio/wak0wfyt.aspx#BKMK_DeclaringAnArray

It says I can initialize an array like this:

Dim doubles = {1.5, 2, 9.9, 18}

But I'm getting error:

Compile error: Expected: end of statement

And it points to the equals sign. I'm merely copy-pasting, what's wrong here?

like image 596
Question Everything Avatar asked May 09 '13 08:05

Question Everything


People also ask

What is expected array error in VBA?

This error has the following cause and solution: The syntax you specified is appropriate for an array, but no array with this name is in scope. Check to make sure the name of the variable is spelled correctly. Unless the module contains Option Explicit, a variable is created on first use.

How do I fix VBA error?

Go to the VBA toolbar and click on Tools and then click on Options. In the Options dialog box, click on the General tab and make sure that within the 'Error Trapping' group, 'Break on Unhandled Errors' is checked.

How do I declare an array in VBA?

Use a Static, Dim, Private, or Public statement to declare an array, leaving the parentheses empty, as shown in the following example. Use the ReDim statement to declare an array implicitly within a procedure. Be careful not to misspell the name of the array when you use the ReDim statement.

Do VBA arrays start at 0 or 1?

By default, an array is indexed beginning with zero, so the upper bound of the array is 364 rather than 365. To set the value of an individual element, you specify the element's index.


1 Answers

This would work in vb or vb.net but not vba. You need to initialise this in two steps.

Dim doubles
doubles = array(1.5, 2, 9.9, 18)
like image 145
glh Avatar answered Nov 09 '22 02:11

glh