Please excuse my ignorance.
If it weren't for ignorance, we'd have nothing to do here, would we? :)
I am trying to declare an array with a variable amount but I'm having no luck. this is what I've tried - Dim Number as string
-
X = Val(Number)
-
dim Array(X) as string
I've also tried
- dim Array(val(Number)) as string
but with both I get a compile error "constant expression required"
Can someone help me. I have tried looking up all my reference books and on the Internet.
For starters, I think it depends on where you define the array. If you are defining it inside a Sub or Function, then I
think (not completely certain) that it has to be a specific size. For a variable-sized array, try defining it at the module level (as Public if desired).
Also, I think (once again, not 100% certain) that you have to define it without any size, then use the
ReDim statement in your code to set the size. For example:
- ' At the module level...
-
Private MyArray() As String
-
-
' In a Sub or Function...
-
ReDim MyArray(1 To X)
-
One important thing to remember - you can change the size of the array later by using ReDim again, but by default it will wipe out all values in the array at the time. Use
Redim Preserve to keep existing contents while changing the size.