-
i use this code:
ReDim Preserve test(UBound(test) + 1) As String
test(UBound(test) + 1) = test
that is inside a public sub and the array test is decleared like this:
Global test() as string
i may have made some minor spelling errors here, thats nt the problem but it gives me the error subscript out of range any idea why?
-
Yup, thats because your array isn't initialized yet (no items in it) And you need to put an on error resume next to handle it
-
You see, the subscript range for an unused array without range declarations is 0-0. That is nothing, so whatever you try to put to it, it'll get you an error.
-
i put the on error thing and it seems to work, but i redimmed it so why did it complain still?
-
not even 0-0 because thats actually one item: 0
it's more like 0 -1
-
hmm, odd, put a break there and then in immediate window:
?UBound(test)
-
You're redimming it with the ubound operator, then adding 1, which is correct in the redim statement, but in the next line, you're accessing it with the ubound operator + 1 again, which will give you an error. Try this instead...
Code:
ReDim Preserve test(UBound(test) + 1) As String
test(UBound(test)) = "test"
or this
Code:
TmpVal=ubound(test)+1
Redim Preserve Test(TmpVal) as String
Test(TmpVal)="Test"
-
i feel like an idiot, i forgot it changed the ubound lol thanks
-
Me.2 I feel stupid, now i need some sleep 6:15 am now