Creare e assegnare dinamicamente valori alle variabili?
-
23-12-2019 - |
Domanda
Ho 264 colonne di dati che ho bisogno di impostare ciascuno in modo indiffidulo come intervallo per memorizzare il valore in una variabile ... quindi sto pensando come:
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
.
(Ho 264 I's, quindi penso che non sia bello farlo ...) Come posso fare questo ?
Ho provato:
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"
.
e ancora non funziona.
è possibile?Voi ragazzi avete qualche suggerimento?Trucchi per fare questo?
Per favore dimmi qualche idea, darò più informazioni possibili per avere almeno una grande soluzione qui.
Grazie.
Soluzione
Prova questo codice:
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
. Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow