|
-
Nov 28th, 2013, 04:38 PM
#1
Thread Starter
Member
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.
-
Nov 28th, 2013, 05:30 PM
#2
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.
-
Nov 28th, 2013, 07:26 PM
#3
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()
-
Nov 29th, 2013, 07:25 AM
#4
Re: subscript out of range, can't see why
'Public' in another module,
a standard module or an object module?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Nov 29th, 2013, 08:15 AM
#5
Lively Member
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.
Last edited by Honduras 2811; Nov 29th, 2013 at 08:18 AM.
-
Nov 29th, 2013, 08:48 AM
#6
Thread Starter
Member
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!
-
Nov 29th, 2013, 10:27 AM
#7
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
-
Nov 29th, 2013, 10:36 AM
#8
Thread Starter
Member
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!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|