변수에 값을 동적으로 만들고 할당 하시겠습니까?
-
23-12-2019 - |
문제
264 개의 데이터 열이있어 변수의 값을 저장할 범위로 각각을 무청하게 설정해야합니다.
For i = 1 To 5
If i = 1 Then
search1 = Sheets(tables).Range(Cells(2, i), Cells(200, i))
ElseIf i = 2 Then
search2 = Sheets(tables).Range(Cells(2, i), Cells(200, i))
ElseIf i = 3 Then
search3 = Sheets(tables).Range(Cells(2, i), Cells(200, i))
ElseIf i = 4 Then
search4 = Sheets(tables).Range(Cells(2, i), Cells(200, i))
ElseIf i = 5 Then
search5 = Sheets(tables).Range(Cells(2, i), Cells(200, i))
End if
Next
.
(나는 264 명, 그래서 나는 그것이 그렇게하는 것이 좋지 않다고 생각한다 ...) 어떻게해야합니까?
시도한 것 :
search & i = Sheets(tables).Range(Cells(2, i), Cells(200, i))
"Compiler error, expected expression"
search(i) = Sheets(tables).Range(Cells(2,i),Cells(200,i))
"Sub or Function not Defined"
.
여전히 작동하지 않습니다.
가능합니까?너희들은 힌트가 있니?이 작업을위한 트릭?
생각을 알려주십시오, 가능한 한 많은 정보를 얻을 수 있습니다.
고맙습니다.
해결책
이 코드를 사용해보십시오 :
Sub test()
Dim i As Integer, n As Integer
Dim tables As String
Dim search() As Variant
n = 264
ReDim search(1 To n)
tables = "Sheet1"
With ThisWorkbook.Sheets(tables)
For i = 1 To n
search(i) = .Range(.Cells(2, i), .Cells(200, i)).Value
Next
End With
End Sub
. 제휴하지 않습니다 StackOverflow