문제 설명
변수를 사용하여 Excel VBA에서 3차원 배열 요소에 액세스 (Use variables to access 3 dimensional array elements in Excel VBA)
변수 값을 기반으로 배열 요소의 값을 읽고 싶습니다.
Public Array1(3, 3) As String
Public Var1 As Integer
Public Var2 As Integer
...
Var1 = 1
Var2 = 1
Array1(1, 1) = "The_string"
MsgBox Array1(Var1, Var2)
에서 The_string을 표시할 것으로 예상합니다.
하지만 그렇지 않습니다. 빈 메시지가 나타납니다. MsgBox Array1(1, 1)
을 시도하면 모든 것이 잘 작동합니다. .Value
와 같은 것을 시도할 때 오류가 발생합니다.
내가 뭘 잘못하고 있는 걸까요? 내 변수 값을 기반으로 배열 항목에 어떻게 액세스할 수 있습니까?
참조 솔루션
방법 1:
Youre just accessing them incorrectly. Let me give you a fee examples of things that do work.
rCell.Offset(0, 1).Value = A(ArCount, 1)
THe above states 1 space of the rCell = A(x,1). Notice how I dont say .Value?
For i = LBound(MyArray, 1) To UBound(MyArray, 1)
j = CLng(((UBound(MyArray) ‑ i) * Rnd) + i)
If i <> j Then
Temp = MyArray(i)
MyArray(i) = MyArray(j)
MyArray(j) = Temp
End If
Next i
This code is a basic random sorting of an array. Good example of array declaration and assignment of variables form that state.
A(count, 5) = arrResults(z, 5)
This one is pretty loaded and hard to describe w/o context. Just another example.
BlockEnd = GradCFArray(j + 1, k)
Another example.
I guess the issue youre having is accessing array contents. An array itself isnt a "Value" but it holds a value. Thats maybe why the .value isnt working for you?
방법 2:
(Posted on behalf of the OP).
Turns out this code works. All I did was restart Excel! Possibly a bug.
(by InnerBushman、Doug Coats、halfer)