Hello all I am getting an error
Run-time error '9':
Subscript out of range
with the following code
Public newarray() As String
Sub test1()
Dim int1 As Integer
int1 = 0
Do
int1 = int1 + 1
newarray(int1) = int1 * 5
Loop Until int1 > 3
End Sub
when i decare the array
Public newarray(4) As string
it works, however I wish to declare a dynamic array. Please help. Thank you.
You can allocate the array size by using
ReDim:This will redefine your entire array. If you need to preserve the contents of the array, and just change the size, you can use
ReDim Preserve newarray(4).Your code assumes that arrays are 1 based. This can be 0 based (set by option in VB). To be sure your code works regardless of this option, you should use
LBound/UBoundfor the limits of your counter: