Is there a way for the array range to be dynamic … like Dim array(1 to N) Double N could be set differently in different parts of the code?
Printable View
Is there a way for the array range to be dynamic … like Dim array(1 to N) Double N could be set differently in different parts of the code?
Is there a way for the array range to be dynamic … like Dim array(1 to N) Double where N could be set differently in different parts of the code?
Paul ... please help me remember how to mark a topic “solved”
Above your first post, there's [Thread Tools] and an option is to mark [SOLVED]
Yes
Look in Help for "ReDim" and "Redim Preserve"
However, I've found that most times it's not needed and can be designed around
Some sample code
Code:Option Explicit
Sub Arrays()
Dim A(1 To 2) As Long ' fixed
Dim B() As Long ' ReDim-able
A(1) = 100
A(2) = 200
MsgBox A(1) & " -- " & A(2)
ReDim B(1 To 3)
B(1) = 1000
B(2) = 2000
B(3) = 3000
MsgBox B(1) & " -- " & B(2) & " -- " & B(3)
ReDim Preserve B(1 To 4)
B(4) = 4000
MsgBox B(1) & " -- " & B(2) & " -- " & B(3) & " -- " & B(4)
Erase A
MsgBox A(1) & " -- " & A(2)
A(1) = 10000
A(2) = 20000
MsgBox A(1) & " -- " & A(2)
End Sub
Paul ... thanks!