Reputation: 27
I am writing a code that uses arrays whose dimension depends of values that the user gives. This value depends on the number of cells that he/she is using and I use this number to do a for-loop structure. Here it´s an example:
Sub Test()
Dim Row As Integer
Dim Myarray(1 To Row) As String
For 1 To Row
'Do whatever I need
Next linha
End Sub
When I tried this, an error appears. Is there any way to use that? I was looking for any clues on google and I found dynamic array. But it dosen´t seem to fit in my problem.
Thanks!
Upvotes: 0
Views: 40
Reputation: 503
I would use a collection instead of an array and you may want to look into doing redim preserve. (https://msdn.microsoft.com/en-us/library/w8k3cys2.aspx)
sub Test(row as range)
Dim my myCollection as new collection
myCollection.add row
For each Item in myCollection
'Do some stuff here...
Next 'Item
end Sub
Upvotes: 0
Reputation: 5770
Change the syntax to (obviously `LengthOfArray' should hold a number with the size of the array):
Sub Test()
Dim Row As Integer
Dim Myarray() As String
n = LengthOfArray
Redim MyArray(1 to LengthOfArray)
For linha = 1 To n
'Do whatever I need
Next linha
End Sub
Upvotes: 1