Consulting

Results 1 to 6 of 6

Thread: For Next lop ends on x +1

  1. #1
    VBAX Mentor
    Joined
    Aug 2012
    Posts
    367
    Location

    For Next lop ends on x +1

    Hi,

    I've noticed that

    Private Sub testloop()
    Dim myrow As Long
    
        For myrow = 1 To 10
        Next myrow
    
        Debug.Print myrow
    End Sub
    gives 11 as the output
    Can anyone explain definitively why this is so? (I'd like to trust this enough to use it as a success/failure test)

    thanks

    ~~~~
    Remember: it is the second mouse that gets the cheese.....

  2. #2
    the For..Next is analogous to

    Do While myrow <= 10
    Loop

    it is incrementing loop (incrementint myrow on every Loop).
    will only stop until myrow > 10.

  3. #3
    VBAX Mentor
    Joined
    Aug 2012
    Posts
    367
    Location
    thanks arneglp

    so ...
    myrow = myrow +1
    if myrow < 11 then do the loop again
    loop
    Remember: it is the second mouse that gets the cheese.....

  4. #4
    mrow = 1
    do until mrow > 10
    mrow = mrow + 1
    loop

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    gives 11 as the output
    Can anyone explain definitively why this is so? (I'd like to trust this enough to use it as a success/failure test)
    The For / Next loop terminates when i > 10

    Since the Next increments the loop variable i, it's last value = 10 + 1 = 11 and it doesn't loop any more

    What are you really trying to do that you want to make this a success/failure test?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    If you don't trust something or don't know if something is right or not, make a test run.
    have an empty Sheet and run the code you're wondering about.


    Sub What_Is_The_Outcome_A()
    Dim i As Long
        For i = 1 To 10
            Cells(i, 3).Value = i
        Next i
    End Sub
    Sub What_Is_The_Outcome_B()
    Dim i As Long
    i = 1
        Do While i <= 10
            Cells(i, 5).Value = i
            i = i + 1
        Loop
    End Sub
    That will give you a visual of the outcome.
    Pretty soon it will be second nature.

Posting Permissions

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