Using arrays in VBA

An array is a set of data items that are stored in contiguous memory locations all of which have the same name and the same data type. A particular data item is referenced by specifying the array and the element position number (the index). Names that are given to arrays are subject to the same rules as any other variable. The elements of the array can be of any data type. For example, Figure 5.5 shows a five-element currency array called expenses. Any one of the elements in this array may be referenced by giving the array name followed by the index in parentheses, (). The index numbering starts at 0, though we tend to think of positions as starting at 1. Thus, the element at position 1 is written as expenses(0). The element at position 2 is written as expenses(1), the element at position 3, expenses(2), and so on. In general the element at position n is written as expenses(n-1).

expenses (0) expenses (1) expenses (2) expenses (3) expenses (4) $20.50 $35.50 $38.25 $65.40 $54.35

Figure 5.5 A five-element array called expenses

0 0

Post a comment