변수를 사용하여 Excel VBA에서 3차원 배열 요소에 액세스 (Use variables to access 3 dimensional array elements in Excel VBA)


문제 설명

변수를 사용하여 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 InnerBushmanDoug Coatshalfer)

참조 문서

  1. Use variables to access 3 dimensional array elements in Excel VBA (CC BY‑SA 2.5/3.0/4.0)

#vba #arrays #excel






관련 질문

열에 문자열이 포함된 경우 열 번호 나열 (List column numbers if columns contain string)

SpinButton SpinUp 또는 SpinDown 새로 고침 (Refreshing SpinButton SpinUp or SpinDown)

변수를 사용하여 Excel VBA에서 3차원 배열 요소에 액세스 (Use variables to access 3 dimensional array elements in Excel VBA)

TODAY() 함수와 월 1일을 기준으로 범위 이동 (Moving a range based on TODAY() function and first of the month)

ALV 레이아웃의 표시된 모든 열을 제거하는 방법 (How to remove all displayed columns of ALV layout)

셀에 입력한 msoShapeOval은 MsoShapeType으로 선언된 변수의 값이 될 수 없습니다. (msoShapeOval entered in cell not allowed to be value for variable declared as MsoShapeType)

Excel VBA의 세로 열에 여러 json을 생성하는 방법은 무엇입니까? (How to generate multiple json in vertical column in Excel VBA?)

공개 함수의 VBA 런타임 오류 -2147319767(80028029) (VBA Run-time error -2147319767 (80028029) in Public Function)

알 수 없는 수의 문자를 패턴 일치시키는 방법 (How to Pattern Match an Unknown Number of Characters)

Excel VBA 파일이 열려 있는지 확인 기능 (Excel VBA Check if File is open function)

ActiveCell..Offset(-1,0)=을 사용하는 If/And/Then/Else 문의 VBA 문제= (VBA Issue with If/And/Then/Else statement using ActiveCell..Offset(-1,0)=)

VBA 런타임 오류 '1004': 응용 프로그램 정의 또는 개체 정의 오류" 설정 셀 내용 (VBA Runtime Error '1004': Application-defined or Object-defined error" setting cell contents)







코멘트