1

I have searched the web, Microsoft's site, vba help, but can't find a reference to what the '0 to 10' part of this dim statement means.

Dim myArray(0 to 10)

Can someone help clarify exactly is going on? I am suspecting it has to do with dimensioning a multidimensional array but I would like to understand what is happening under the covers or if I am even correct.

Edit: Sorry, I started thinking about this question in the context of redimensioning, which is where I first saw the example, but then saw that its use was also for the initial dimensioning as well.

Rob
  • 161
  • 1
  • 1
  • 10
  • @Okuma.Scott: The linked question does not describe the "0 to 10". – Werner Henze Oct 31 '13 at 10:53
  • @Walter Henze My mistake. I realized it the second after flagged too. REALLY wish there was a way to take back a flag in the first min or so. Plenty of people agree on meta. – Scott Solmer Oct 31 '13 at 11:37
  • I realize the way I originally wrote this question did not make clear what I was asking. I have edited it in an attempt to provide that clarity. I apologize for my pathetic excuse of a first attempt to ask my question. – Rob Oct 31 '13 at 16:38

2 Answers2

1

This statement is declaring an empty variant array of 11 items ( 0 to 10 ) called myArray.

It is a variant because is not specifically declared as any other type.

Craig T
  • 2,761
  • 5
  • 25
  • 33
  • 1
    After you have declared it, you can use the variable like this: `myArray(0)="hello!"` and `myArray(10)=2.5`. If you use an index less than 0 or greater than 10 it will cause an error. If you access an element that has not been initialized (explicitly assigned a value) it will return "nothing". Note that VBA is a bit weird about the base of arrays - in many cases it ignores the lower bound of arrays... – Floris Oct 31 '13 at 01:21
  • How is this different from `dim myArr(10)`? – Rob Oct 31 '13 at 01:36
  • @Rob .. myArr(10) has only 10 elements .. 0 to 9 .. – matzone Oct 31 '13 at 01:44
  • Just to note, it's an array of Variants, not a Variant array. http://msdn.microsoft.com/en-us/library/office/aa163618%28v=office.10%29.aspx – Doug Glancy Oct 31 '13 at 02:22
  • @matzone not necesarly, it depends on your Option Base setting, if it is one then you are correct, myArr(10) will have only 10 elements, the default `Option Base 0` will result in 11. It's always better to declare the lower bound – SWa Oct 31 '13 at 13:10
0

http://excelvbatutor.com/vba_chp21.htm

The website has quite detailed explanation of arrays in VBA.

In brief,

Dim myArray(0 to 10) is creating an array with index 0 to 10 (i.e. 11 elements)

For what you mentioned about redimensioning, you should look for redim instead

sam092
  • 1,325
  • 1
  • 8
  • 8