subscript out of range, can't see why
I get <subscript out of range> as a tooltip when debugging this (bold) code. It's probably obvious, but I can't see why.
Test() As String and H% are both listed with other (working) variables as 'Public' in another module, including Z_max, Z_Inside, Tmp1.
For H = 0 To Z_max
Test(H) = "Z " & Z_Inside
Tmp1 = Tmp1 + Test(H)
Z_Inside = Z_Inside +2
Next H
I didn't think I missed anything out, but I must have done.
Re: subscript out of range, can't see why
The For variable H shadows the public H, try to use different name for For counter.
Re: subscript out of range, can't see why
Well, what is the value of H when the error occurs?
What are the upper and lower bounds of Test()?
What is the value of Z_Max?
Can't tell much about whats posted there but I would say that either your array TEST() starts at 1 or Z_max is a higher value than the upper index of TEST()
Re: subscript out of range, can't see why
Quote:
'Public' in another module,
a standard module or an object module?
Re: subscript out of range, can't see why
Code:
Test(H) = "Z " & Z_Inside
Have you tried converting Z-Inside to a string? If I remember right, you declared it as a numeric value.
However, reading the 0th element of an array can often cause problems.
Look at it this way: Dim straryPoo() as string
At this point you have indicated that you are creating an array, but, if you start asking for values from it, you should get back vbNulls.
I don't think that straryPoo() will have any value until you start (Redim Preserving)
Why do I feel this a valid comment? In the old days I would have used the 'brute force' method to figure out what was wrong with the loop I wrote today for a recursion experiment.
Today, I sat back and looked at what I thought I was trying to do. The solution was obvious, after about 5 minutes.
Remember, it often takes a lot less time to program smart than to type a lot.
Re: subscript out of range, can't see why
I tried another counter, as used in a previous loop in the same module, no joy. I tried replacing 0 with 1, and Z_max with 50, which is the highest number it can have assigned to it. Got the same error with H = 0, and 1. H and other counter etc, all public in a standard module. Upper and lower bounds of Test() not specified in Dim.
When I changed "Test() As String" to "Test (1 To 200) As String" in The 'Public' Dim, ie specifying a number way beyond Z_max, the routine ran fine, when I specified Z_max as the upper bound, as in original code. So it didn't like having no array size defined in the 'Dim' statement.
What I don't understand is, when all my other For Next loops using arrays are Dim'd and coded in the same way, in the same modules, why this one won't work unless I specify an array size in the Dim statement.
It works now (with Z_Inside as an Int), which is fine, but I still can't see why it didn't before. Also, it would just be better code if I didn't have to reserve memory space for all those unused Array elements. I could change 200 to 50, which would be less extravagant.
straryPoo()... that's a great name for an array, I won't forget that one in a hurry!
Re: subscript out of range, can't see why
You have to specify an array size somewhere. It does not have to be in the Dim but it does have to be before you try to access it
This is ok
Code:
Dim MyArray() as string
Redim MyArray(25)
MyArray(1)="Test"
This is Ok
Code:
Dim MyArray(25) as string
MyArray(1)="Test"
This is not ok
Code:
Dim MyArray() as string
MyArray(1)="Test"
Will give index out of bounds as there are no elements defined in MyArray as of yet
Re: subscript out of range, can't see why
Dim MyArray() as string
Redim MyArray(Z_max)
MyArray(1)="Test"
So that would be ok, then? Will test, when at home again...
Thanks!