本文为:《别怕,Excel VBA其实很简单(第3版)》随书问题参考答案

下面是解决本例问题的参考过程:

Sub 求三维数组的元素个数()
'=====以下代码用于声明三维数组并给三维数组赋值
Dim arr(1 To 3, 1 To 5, 1 To 10) As Integer
Dim i As Integer, j As Integer, k As Integer, m As Integer
For i = 1 To 3
For j = 1 To 5
For k = 1 To 10
m = m + 1
arr(i, j, k) = m
Next
Next
Next
'====以下代码用于求三维数组中的元素个数
'****求数组第1维的最大和最小索引号
Dim a As Integer, b As Integer
a = UBound(arr, 1)
b = LBound(arr, 1)
'****求数组第2维的最大和最小索引号
Dim c As Integer, d As Integer
c = UBound(arr, 2)
d = LBound(arr, 2)
'****求数组第3维的最大和最小索引号
Dim e As Integer, f As Integer
e = UBound(arr, 3)
f = LBound(arr, 3)
'****用对放框显示数组的元素个数
MsgBox "数组包含的元素个数是:" & (a - b + 1) * (c - d + 1) * (e - f + 1)
End Sub

声明:本站所有文章均出自互联网,只为学习之用;如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。