1. 数组的声明和使用:
Dim myArray(1 To 5) As Integer '声明一个包含5个整数的数组
'给数组赋值
myArray(1) = 10
myArray(2) = 20
myArray(3) = 30
myArray(4) = 40
myArray(5) = 50
'访问数组元素
MsgBox myArray(3) '将显示30
2. For 循环:
'For 循环从1到5,逐一处理数组元素
For i = 1 To 5
MsgBox myArray(i)
Next i
3. For Each 循环:
Dim element As Variant
'For Each 循环遍历数组中的每个元素
For Each element In myArray
MsgBox element
Next element
4. Do While 循环:
Dim counter As Integer
counter = 1
'Do While 循环,当条件满足时执行
Do While counter <= 5
MsgBox myArray(counter)
counter = counter + 1
Loop
这些只是基础示例,你可以根据实际需求进行调整和扩展。VBA提供了多种循环和数组处理的方式,选择合适的方法取决于你的具体情况。
转载请注明出处:http://www.pingtaimeng.com/article/detail/6692/VBA