静态数组是具有确定大小的数组。当你事先知道数组的大小时使用静态数组。静态数组的大小是在数组的声明语句里确定的,例如,语句DimFruits(10)AsString声明了一个由10个成员组成的叫做Fruits的静态数组。
动态数组是大小可以改变的数组。如果数组的大小每次都由程序运行而决定的话,就使用动态数组。
Sub DynArray( )
Dim counter As Integer
'declare a dynamic array
Dim myArray( ) As Integer
'specify the initial size of the array
Redim myArray(5)
Workbooks.Add
'populate myArray with values
For counter = 1 to 5
myArray(counter) = counter +1
ActiveCell.Offset(counter-1, 0).Value = myArray(counter)
Next
'change the size of myArray to hold 10 elements
Redim Preserve myArray(10)
'add new values to myArray
For counter = 6 To 10
myArray(counter) = counter * counter
With ActiveCell.Offset(counter-1, 0)
.Value = myArray(counter)
.Font.Bold = True
End with
Next counter
End Sub
在过程DynArray里,Dim myArray() As Integer语句声明了一个叫做myArray的动态数组。尽管该语句声明了数组,但是,没有分配任何内存给该数组。第一条ReDim语句明确了myArray的开始大小并且占据了10个字节的内存让它储存5个成员,正如你所知,每个整型数据需要两个字节的内存。