VBA数组用法-简易实例
1、VBA数组的定义方法
下面是几种数组常用的定义方法,一维数组的定义、二维数组的定义
直接赋值定义、调用Array函数定义、调用Excel工作表内存数组
''''''''''''直接定义给数组赋值
'一维常量数组的定义
Sub arrDemo1()
Dim arr(2) As Variant '数组
arr(0) = "vba"
arr(1) = 100
arr(2) = 3.14
MsgBox arr(0)
End Sub
'二维常量数组的定义
Sub arrDemo2()
Dim arr(1, 1) As Variant 'Dim arr(0 To 1, 0 To 1) As Variant
arr(0, 0) = "apple"
arr(0, 1) = "banana"
arr(1, 0) = "pear"
arr(1, 1) = "grape"
For i = 0 To 1
For j = 0 To 1
MsgBox arr(i, j)
Next
Next
End Sub
''''''''''''用array函数创建常量数组
'一维数组
Sub arrayDemo3()
Dim arr As Variant '数组
arr = Array("vba", 100, 3.14)
MsgBox arr(0)
End Sub
'二维数组
Sub arrayDemo4()
Dim arr As Variant '数组
arr = Array(Array("张三", 100),