Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

#@!*$# Subscript out of range error 1

Status
Not open for further replies.

navyguy

Programmer
Aug 10, 2002
30
US
I would say I am not a beginner but also not an expert. I try to dimension things properly and use dynamic arrays when appropriate. Just when I think I have things figured out I get a "Subscrit out of range" error and the reason is not very clear. For example, I get an error from the code below. There are lots of reasons one might get the "subscript range" error without a clear reason. It would help us mid-level programmers out a lot if some of the more experienced programmers could contribute to this string by citing intances they have come across where this error message has come up where the reason isn't immediately obvious. I, for one, would be very grateful.

Sub test()
Dim myarray()
Dim i As Integer
For i = 1 To 100
myarray(i) = i
Next i
End Sub
 
>Dim myarray()


At this point all you have done is tell VBA that you have a variable that you want as a (variant) array. You have not, however, said how big you want that array, so at this stage VBA allocates no storage space for it.

>myarray(i) = i

At this point you try to assign a value to an element in the array, but (as we saw from above) VBA has assigned no space for storage, and hence no elements for you to assign to. Bingo, an error message.

So, you either need to dimension the array with the correct upper bound (which means VBA knows how big to make it):

Dim myarray(100)

(although actually this will reserve space for elements 0 thru 100; check out the Option Base statement)

However, you mention an interest in dynamic arrays. In which case, you actually need to do a Redim at an appropriate point. And you may not want to lose the current contents of the array when you do this, so you may want to consider the Redim Preserve syntax

One further point: you may want to be specific about the data type of the array, otherwise it will default to a variant - which may not be what you want.

So here's on possible version of your code:

Dim myarray() As Integer
Dim i As Integer
For i = 1 To 100
ReDim Preserve myarray(i)
myarray(i) = i
Next i
 
strongm:

Great response, you covered all of the major points. But, you should look at your redim statement location. Placing it inside the for loop changes the array dimensions 100 times. Placing the statement outside the loop with the same bounding conditions as the loop would save a lot of overhead.

Ron
 
Right Ron since the number is fixed, there is no need for a dynamic array.
Plus: I haven't found it in the Help file and haven't used it since school, but can't you fill an array with increasing numbers like this:

myArray=Array(1..100)

?

This would make any loop or redim superfluous.
;-)

Andreas Galambos
EDP / Technical Support Specialist
(andreas.galambos@bowneglobal.de)
HP:
 
Quite so. The original question expressed an interest dynamic arrays. As I said in my answer, if you use dynamic arays, then the redim needs to take place at the appropriate point.

The given code, however, is not an example of best practice; as has been observed, if you know the upper bound of the array you wouldn't really need a dynamic array or, if the upper bound was calculated at run-time, you might want to put the redim outside of the loop. However, for illustrative purposes on this issue the example is fine.
 
>can't you fill an array with increasing numbers like this

IN VB/VBA sadly not. I vaguely recall (it's a long time since I used either) that this was something you could do with both QuickBasic and GW Basic
 
Oh! That's why I didn't find it... Hmpff [cannon]
Thanks for the input strongm.
[afro2]
 
Hi,

How do I create a dynamic array without getting the "Subscript out of range" error? This is my code, which attempts 3 different ways of defining the array, but all give errors - for the first two: "Subscript out of range error" and the third one: "Type mismatch error".

dim hover()
dim i

if highlight="qual" then
ReDim hover(5)
ReDim Preserve hover("10")
ReDim Preserve hover("7")
ReDim Preserve hover("6")
ReDim Preserve hover("8")
ReDim Preserve hover("11")
ReDim Preserve hover("11")

elseif highlight="loss" then
ReDim hover("7","8","11","16","5")

elseif highlight="shel" then
hover =Array("6","13","17","11","14","15","18","4")

end if

For each i in hover()
Response.Write hover(i)
Next

I am quite new to asp so any help would be gratefully recieved!
Thanks,
Louise
 
erm - this is a VBA forum - not ASP.....

Rgds, Geoff
[blue]Si hoc signum legere potes, operis boni in rebus Latinus alacribus et fructuosis potiri potes![/blue]
Want the [red]best[/red] answers to your questions ? faq222-2244
 
But ASP uses VBScript by default and that is what I am using...
thanks anyway...
 
But this is not a VBScript forum either - VBA. I don't mean to cause offense or give the impression that you are not welcome - simply that I don't know if anyone in this forum knows anything about ASP. I'm pretty sure there is a dedicated ASP forum here somewhere and several VB fora - you may get more answers there.

Rgds, Geoff
[blue]Si hoc signum legere potes, operis boni in rebus Latinus alacribus et fructuosis potiri potes![/blue]
Want the [red]best[/red] answers to your questions ? faq222-2244
 
xlbo is quite correct. Both of these forums exist

For ASP, I suggest forum333
For VBScript, I suggest forum329

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Another alternative to Dynamic Arrays is to use a Collection.

Sub test2()
Dim colTest As New Collection
Dim i As Integer
For i = 1 To 100
colTest.Add i
Next i

MsgBox colTest.Count & " objects in collection"

End Sub

This way no redim is necesaary. An advantage of a collection is that the members do not have to be the same data type. As with any application, you need to select the proper object for the function. I don't know what your application does so an array may be more suitable. This is just something to store in the back of your mind because collections are very usefull.

Scott
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top