Consulting

Results 1 to 7 of 7

Thread: Sleeper: Cleaner Code

  1. #1
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location

    Sleeper: Cleaner Code

    Is there a way to branch to the end of a For loop without extra statements? In FORTRAN, every Do Loop ended with a statement number so it was easy to branch to the bottom of the loop. For example

    Do 10 I = 1 to 5
    if something go to 10

    10 Continue

    In VBA, you can accomplish the same thing with, say,

    For I = 1 To 5
    If  something Then goto NextI
    NextI:
        Next I
    Yes, I know that with the proper use of If statments, this kind of branching is not necessary. But humour me ... Is there a way to branch directly to the end of the For loop?
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

  2. #2
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    268
    Location
    Exit For

  3. #3
    Curious that you should ask about this because I have oft wondered the same thing. If I understood your question, you are looking for something that (in effect) says "do the next iteration". I've always used (reluctantly) the "GoTo NextI:" approach, but this seems to be a feature that's been omitted from the language. In many cases you can use an "If...Then" modification to the logic to accomplish the desired result, but sometimes that gets REALLY awkward and hard to read.

  4. #4
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    268
    Location
    Oops! Sorry... misread your post!

  5. #5
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Unfortunately, there is nothing like LoopNext or NextFor or anything like that. I've used things like this before:

    For i = 1 To 100
    Do
    'code i want to repeat with current value of i until something happens
    If WhatIWantToHappenHappens Then
    Exit Do
    End If
    Loop
    Next 'i
    but generally if you have good if blocks and the occasional boolean variable to test, you shouldnt need it. I never use gotos anymore though, doesn't look right and seems like i just threw the code together
    Matt

  6. #6
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    Quote Originally Posted by BlueCactus
    [vba]Exit For[/vba]
    thanks, but Exit For will exit the loop. I do not want to do that. Rather I want to drop to the bottom, increment the index, ...

    and I should have read all the replies ...
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

  7. #7
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    Quote Originally Posted by Cyberdude
    Curious that you should ask about this because I have oft wondered the same thing. If I understood your question, you are looking for something that (in effect) says "do the next iteration". I've always used (reluctantly) the "GoTo NextI:" approach, but this seems to be a feature that's been omitted from the language. In many cases you can use an "If...Then" modification to the logic to accomplish the desired result, but sometimes that gets REALLY awkward and hard to read.
    I agree. It can be really awkward and defeats the original purpose of "not using go to", i.e., the code becomes very hard to read.
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the 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
  •