数组变量:有时需要向一个单一的变量赋于多个值,那么您可以创建一个可包含一系列值的变量,这种变量被称为数组。
'静态数组 dim a(2) a(0)="George" a(1)="john" a(2)="Ethon" for i =0 to 2 msgbox a(i) next for i =0 to ubound(a) 'ubound函数,返回指定数组维数的最大可用下标 msgbox a(i) next
'动态数组 dim a() for i = 0 to 2 redim preserve a(i) 'ReDim语句,用于过程级声明动态数组变量并分配或重新分配存储空间 a(i)=i+1
msgbox a(i) next
'动态二维数组 Dim MyArray() '首先定义一个一维动态数组 ReDim MyArray(1,1) '重新定义该数组的大小 MyArray(0,0) = "A" '分别为数组赋值 MyArray(0,1) = "a" MyArray(1,0) = "B" MyArray(1,1) = "b" ReDim Preserve MyArray(1,2) '重新定义该数组的大小 MyArray(0,2) = "A-a" '继续为数组赋值 MyArray(1,2) = "B-b" MsgBox UBound(MyArray,1) MsgBox UBound(MyArray,2) For i=0 To UBound(MyArray,1) For j=0 To UBound(MyArray,2) MsgBox MyArray(i,j) '循环遍历数组,并输出数组值 Next Next
Dim MyArray() '首先定义一个一维动态数组 ReDim MyArray(3) '重新定义该数组的大小 MyArray(0) = "我" '分别为数组赋值 MyArray(1) = "要" MyArray(2) = "学" MyArray(3) = "习" ReDim Preserve MyArray(5) '重新定义该数组的大小 MyArray(4) = "测" '继续为数组赋值 MyArray(5) = "试" For i=0 To UBound(MyArray) MsgBox MyArray(i) '循环遍历数组,并输出数组值 Next