PDA

View Full Version : Solved: Do While - exit conditions



MamboKing
07-04-2008, 05:16 PM
Dim theMatrix(1 To 100) As Double
Dim idxRow As Integer

Do While theMatrix(idxRow) = m
....
....
idxRow = idxRow - 1
If idxRow = 0 Then Exit Do
Loop

It happens that although idxRow goes to zero (Exit condition) the VBA still checks the While conditions before exiting.

In fact, the error "Subscript out of range" pops up and it refers to the check of While theMatrix(0). Of course such index is invalid for theMatrix.

Did anyone experice something similar ever?

Hoping for some feedback. Thanks.

Bob Phillips
07-05-2008, 03:32 AM
It happens. If you can have more than one exit condition you have to test both



Dim theMatrix(1 To 100) As Double
Dim idxRow As Integer

Do While theMatrix(idxRow) = m And idxRow > 0
....
....
idxRow = idxRow - 1
Loop