11.08.2013 Views

Excel's Formula - sisman

Excel's Formula - sisman

Excel's Formula - sisman

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

646<br />

Part VI: Developing Custom Worksheet Functions<br />

Dim MyArray(1 To 100) As Long<br />

When you declare an array, you need to specify only the upper index, in which case VBA (by<br />

default) assumes that 0 is the lower index. Therefore, the following two statements have the<br />

same effect:<br />

Dim MyArray(0 to 100) As Long<br />

Dim MyArray(100) As Long<br />

In both cases, the array consists of 101 elements.<br />

If you want VBA to assume that 1 is the lower index for all arrays that declare only the upper<br />

index, include the following statement before any procedures in your module:<br />

Option Base 1<br />

If this statement is present, the following two statements have the same effect (both declare an<br />

array with 100 elements):<br />

Dim MyArray(1 to 100) As Long<br />

Dim MyArray(100) As Long<br />

Declaring multidimensional arrays<br />

The array examples in the preceding section are one-dimensional arrays. VBA arrays can have up<br />

to 60 dimensions, although it’s rare to need more than 3 dimensions (a 3-D array). The following<br />

statement declares a 100-integer array with two dimensions (2-D):<br />

Dim MyArray(1 To 10, 1 To 10) As Long<br />

You can think of the preceding array as occupying a 10 x 10 matrix. To refer to a specific element<br />

in a 2-D array, you need to specify two index numbers. For example, here’s how you can assign a<br />

value to an element in the preceding array:<br />

MyArray(3, 4) = 125<br />

A dynamic array does not have a preset number of elements. You declare a dynamic array with a<br />

blank set of parentheses:<br />

Dim MyArray() As Long

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!