Dynamic Arrays

A dynamic array has a variable number of elements and can be enlarged or shrunk while the program is running. The syntax for declaring a dynamic array is the same as for static arrays except that no indices are specified—the parentheses are left blank:

Dim arrayname() As type

Before you can use the array, you must set its size using the ReDim statement:

ReDim arrayname(indices)

The indices argument specifies both the number of dimensions and the number of elements, using the same syntax as you learned previously for declaring static arrays. Some examples are

 Dim Array1() Dim Array2() Dim Array3() ... Redim Array1(10) ' 1 dimension, 11 elements 0-10. Redim Array2(5 to 20) ' 1 dimension, 16 elements 5-20. ReDim Array3(1 To 5, ...

Get Office® XP Development with VBA now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.