Microsoft Office Tutorials and References
In Depth Information
In VBA, x equals –25 after this statement is executed:
x = –5 ^ 2
VBA performs the exponentiation operation first, and then applies the negation operator. The following
statement returns 25:
x = (–5) ^ 2
Using Arrays
An array is a group of elements of the same type that have a common name; you refer to a specific element in
the array by using the array name and an index number. For example, you may define an array of 12 string vari-
ables so that each variable corresponds to the name of a different month. If you name the array MonthNames,
you can refer to the first element of the array as MonthNames(0), the second element as MonthNames(1), and
so on, up to MonthNames(11).
Declaring an array
You declare an array with a Dim or Public statement just as you declare a regular variable. You also can specify
the number of elements in the array. You do so by specifying the first index number, the keyword To, and the
last index number — all inside parentheses. For example, here's how to declare an array comprising exactly 100
numbers (of data type Long):
Dim MyArray(1 To 100) As Long
When you declare an array, you need to specify only the upper index, in which case VBA (by default) assumes
that 0 is the lower index. Therefore, the following two statements have the same effect:
Dim MyArray(0 to 100) As Long
Dim MyArray(100) As Long
In both cases, the array consists of 101 elements.
If you want VBA to assume that 1 is the lower index for all arrays that declare only the upper index, include the
following statement before any procedures in your module:
Option Base 1
If this statement is present, the following two statements have the same effect (both declare an array with 100
Dim MyArray(1 to 100) As Long
Dim MyArray(100) As Long
Declaring multidimensional arrays
Search JabSto ::

Custom Search