Sub test1001()
'静态数组的方法定义三维数组
Dim arr1(3, 3, 3)
N = 1
For I = 1 To 3
For J = 1 To 3
For K = 1 To 3
arr1(I, J, K) = N
Debug.Print "arr1(" & I & "," & J & "," & K & ")=" & arr1(I, J, K)
N = N + 1
Next
Next
Next
End Sub
Sub test1002()
'动态数组的方法定义三维数组,如果redim需要用到变量,把redim放在循环体里面会好一些
'因为相当于每次循环都在灵活的 又重新redim
Dim arr2()
m = 2
For I = 1 To 3
For J = 1 To 3
For K = 1 To 3
'这里的redim有意义,因为具体的值会替代变量
ReDim arr2(I, J, K)
arr2(I, J, K) = m
Debug.Print "arr2(" & I & "," & J & "," & K & ")=" & arr2(I, J, K)
m = m + 2
Next
Next
Next
Debug.Print
End Sub
Sub test1003()
'动态数组的方法定义三维数组
'如果再循环外面,redim 数组会比较麻烦
Dim arr3()
a = 3
I = 3
J = 3
K = 3
ReDim arr3(I, J, K)
For I = 1 To 3
For J = 1 To 3
For K = 1 To 3
arr3(I, J, K) = a
Debug.Print "arr3(" & I & "," & J & "," & K & ")=" & arr3(I, J, K)
a = a + 3
Next
Next
Next
Debug.Print
End Sub
Sub test1004() '错误示例,能看出 arr3(1,1,2)会报错了!
'动态数组的方法定义三维数组
'如果再循环外面,redim 数组会比较麻烦需要一定redim准确,或足够大
'相当于dim为静态了,否则里面还要继续redim 不然就报错
Dim arr3()
a = 3
I = 1
J = 1
K = 1
'如果不在redim之前给变量赋值,相当于变量还是为空,这种redim是无意义的
ReDim arr3(I, J, K)
For I = 1 To 3
For J = 1 To 3
For K = 1 To 3
arr3(I, J, K) = a
Debug.Print "arr3(" & I & "," & J & "," & K & ")=" & arr3(I, J, K)
a = a + 3
Next
Next
Next
Debug.Print
End Sub
Sub 测试1()
Dim arr1
arr1 = Array(Range("a1:a4"), Range("b1:b4"), Range("c1:c4"))
Rem array()转的都是1维数组把, no 这里是2维升3维数组
Rem array 是增加一维,并非是转成1维数组了
Debug.Print "arr1(0)(1)(1)=" & arr1(0)(1)(1)
For I = 1 To UBound(arr1)
Debug.Print "arr1(" & I & ")(2)(1)=" & arr1(I)(2)(1) '写成arr(i)会报类型不匹配,因为数组维数不对
Next I
'不确定这是不是三维数组
'我理解arr1=array(),那么arr1(0)就是第1个数组,range("a1:a4")
'这种特殊写法arr1(0)(1)(1),我理解为是第1个数组元素的,range里的第1行第1列
For Each I In arr1(0)
Debug.Print I;
Next
Debug.Print
For Each I In arr1(1)
Debug.Print I;
Next
Debug.Print
For Each I In arr1(2)
Debug.Print I;
Next
Debug.Print
End Sub
三维数组的表示问题,和之前的不同了
Sub test_3d()
Dim arr1(1 To 2)
Dim arr2(1 To 2, 1 To 2)
Dim arr3(1 To 2, 1 To 2, 1 To 2)
Dim arr4
arr1(1) = 1
arr2(1, 1) = 10
arr3(1, 1, 1) = 100
'arr4(1)(1)(1) = 100 '错误
Debug.Print arr1(1)
Debug.Print arr2(1, 1)
Debug.Print arr3(1, 1, 1)
'Debug.Print arr4(1)(1)(1)
End Sub
Sub test1()
Dim arr1
Dim arr2
Dim arr3
Dim arr4
Dim arr5(3, 4, 5)
arr1 = Range("b1:b3")
Debug.Print arr1(3, 1)
'Debug.Print arr1(3)(1)
arr2 = Range("b1:b3").Value
Debug.Print arr2(3, 1)
'Debug.Print arr2(3)(1)
arr3 = Array(Range("a1:a3"), Range("b1:b3"))
'Debug.Print arr3(0, 1, 1)
Debug.Print arr3(1)(1)(3)
'arr4 = Array(Range("a1:a3"), Range("b1:b3")).Value '报错
''Debug.Print arr4(0, 1, 1)
'Debug.Print arr4(1)(1)(3)
arr5(0, 0, 0) = 111
Debug.Print arr5(0, 0, 0)
'Debug.Print arr5(0)(0)(0)
End Sub