Créer et attribuer de manière dynamique des valeurs aux variables?
-
23-12-2019 - |
Question
J'ai 264 colonnes de données que j'ai besoin de définir chacune d'une plage pour stocker la valeur dans une variable ... Donc, je pense comme suit:
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
(j'ai 264 ans, je pense que ce n'est pas bon de faire ça ...) Comment puis-je faire ceci ?
J'ai essayé:
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"
Et toujours ne fonctionne pas.
est-ce possible?Est-ce que vous avez des allusions?astuces pour faire cela?
S'il vous plaît dites-moi des pensées, je donnerai autant d'informations que possible d'avoir au moins une excellente solution ici.
merci.
La solution
Essayez ce code:
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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow