Criar e atribuir valores dinamicamente a variáveis?
-
23-12-2019 - |
Pergunta
Tenho 264 colunas de dados que preciso definir cada uma individualmente como um Range para armazenar o valor em uma variável...então estou pensando como:
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
(Eu tenho 264 I's, então acho que não é bom fazer isso ...) Como posso fazer isso?
Eu tentei:
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 ainda não funciona.
Isso é possível ?Vocês têm alguma dica?truques para fazer isso?
Por favor, diga-me o que pensa, darei o máximo de informações possível para ter pelo menos uma ótima solução aqui.
Obrigado.
Solução
Experimente este código:
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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow