Subscript Out of Range Error

K

Kurt Garnjost

In the following code, while looping through the array,
dblNumbers, the element reference suddenly goes from 7 to
366, when the referenced array has only 9 items, causing
a "Subscript Out of Range" error.

For Each i In dblNumbers
If dblNumbers(i) = dblExNum Then
booAlrdyUsed = True
Exit For
End If
Next i

The referenced array has only 9 items at this point. Can
anyone explain this behavior?

The following code is work around:

For i = 0 To UBound(dblNumbers)
If dblNumbers(i) = dblExNum Then
booAlrdyUsed = True
Exit For
End If
Next i

However, I would very much like to know why the other
approach is failing. It is in a While ... Wend loop,
which periodically adds an element to the array. The
array has nine elements at the point at which the problem
occurs. There is nothing unusual about the point in the
Word document, where the problem crops up. It is the same
as the 15 or so items processed before this point without
complaint.

Kurt Garnjost
 
P

Peter Hewett

Hi Kurt Garnjost

The structure of the first loop appears to be incorrect. I would expect the code using a
For Each loop to look something like this:

Dim booAlrdyUsed As Boolean
Dim adblNumbers() As Double
Dim dblExNum As Double
Dim dblEnumerator As Double

ReDim adblNumbers(0 To 8)

For Each dblEnumerator In adblNumbers
If dblEnumerator = dblExNum Then
booAlrdyUsed = True
Exit For
End If
Next

HTH + Cheers - Peter
 
K

Kurt Garnjost

Peter,

Thanks for the reply.

I left out the Dim and ReDim statements in my example, but
they were there in the original code.

First, doesn't your code do a comparison between the array
index (dblEnumerator) and dblExNum, instead of comparing
thedblExNum to the value stored at the the array element
identified by the index, which is what I wanted?

Second, I even if I used your code, the problem I was
having was that the index in the For Each ... Next loop
was skipping from 7 to 366 in an array dimensioned with 9
elements.

Any additional thoughts?

Kurt
-----Original Message-----
Hi Kurt Garnjost

The structure of the first loop appears to be incorrect.
I would expect the code using a
 
G

George Nicholson

This is an apples to oranges problem.

Using "dblNumbers(i) = dblExNum" the way you do implies that you expect i to
represent the *position* of an element in the array.

"For i = 0 To UBound(dblNumbers)" does this.
"For Each i In dblNumbers" does not. In this case i represents the *value*
of each item in the array (7,366, ...etc), not it's position.

Either use your "workaround" code or change "If dblNumbers(i) = dblExNum
Then" to "If i = dblExNum Then" so that you compare apples to apples
 
K

Kurt Garnjost

George,

OK. Thanks very much. The work around is fine for now.
At some point I will see if I can understand the behavior
I was seeing and the error message based on now
understanding that "i" in this case is the value of an
arrary item.

Kurt
 
P

Peter Hewett

Hi Kurt Garnjost

No my code compares the array value, don't confuse the enumerator with the loop index.
The enumerator for arrays and collections is the object not an index.

Try the code!

HTH + Cheers - Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top