'向VBA数组中写入数据
'1、按编号(标)写入和读取
Sub t1() '写入一维数组
Dim x As Integer
Dim arr(1 To 10)
arr(2) = 190
arr(10) = 5
End Sub
Sub t2() '向二维数组写入数据和读取
Dim x As Integer, y As Integer
Dim arr(1 To 5, 1 To 4)
For x = 1 To 5
For y = 1 To 4
arr(x, y) = Cells(x, y)
Next y
Next x
MsgBox arr(3, 1)
End Sub
'2、动态数组
Sub t3()
Dim arr()
Dim row
row = Sheets("sheet2").Range("a65536").End(xlUp).row - 1
ReDim arr(1 To row)
For x = 1 To row
arr(x) = Cells(x, 1)
Next x
Stop
End Sub
'3、批量写入
Sub t4() '由常量数组导入
Dim arr
arr = Array(1, 2, 3, "a")
Stop
End Sub
Sub t5() '由单元格区域导入
Dim arr
arr = Range("a1:d5")
Stop
End Sub
vba给数组赋值
最新推荐文章于 2024-10-09 16:06:27 发布