新建From1(窗体),新建Command1(按钮CommandButton),代码:
Private Sub Command1_Click()
Dim i As Long
Dim arrB() As Byte
ReDim arrB(3)
For i = 0 To UBound(arrB)
Debug.Print i
ReDim arrB(5)
Next
Debug.Print "------"
i = 0
Dim intN As Integer
intN = 4
For i = 0 To intN
Debug.Print i
intN = 8
Next
End Sub
运行结果:
0
1
2
3
------
0
1
2
3
4
看来For i的时候已经固化,不会修改i的范围,所以无需把UBound(arrB)是先做成一个变量。做成变量反而需要更多的代码和CPU时间。
按一步步运行发现,不会到For那行,所以不会影响。