Consulting

Results 1 to 2 of 2

Thread: Solved: Do While - exit conditions

  1. #1

    Solved: Do While - exit conditions

    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.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It happens. If you can have more than one exit condition you have to test both

    [vba]

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

    Do While theMatrix(idxRow) = m And idxRow > 0
    ....
    ....
    idxRow = idxRow - 1
    Loop
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •