VBA 计算数组的最大索引和最小索引
计算数据的最大索引和最小索引分别用 Ubound 和 LBound 函数。对于多维索引需要指定数据的目标维度。
1、一维数组
Sub arr_idx_cnt()
Dim arr(23 To 72) '定义数组
Static min_idx, max_idx, n As Integer, msg As String '声明本地变量
min_idx = LBound(arr)
max_idx = UBound(arr)
n = max_idx - min_idx + 1
msg = "数组的最小索引号是:" & min_idx & Chr(13) _
& "数组的最大索引号是:" & max_idx & Chr(13) _
& "数组的元素个数是:" & n
MsgBox msg
End Sub
运行代码,弹出下边的消息框
2、多维数组
Sub arr_idx_cnt()
Dim arr(23, 72) '定义二维数组
Static min_idx_a, max_idx_b, n As Integer, msg As String '声明本地变量
min_idx_a = LBound(arr, 1)
max_idx_b = UBound(arr, 2)
msg = "数组第一维度的最小索引号是:" & min_idx_a & Chr(13) & _
"数组第二维度的最大索引号是:" & max_idx_b
MsgBox msg
End Sub
运行代码,弹出下边的消息框
非学无以广才,非志无以成学。