How to exit if statement and not continue with Do Loop 
Author Message
 How to exit if statement and not continue with Do Loop

Hello!

Have a routine that looks similar to this:

Sub test_routine

lc = 1

do until cells(lc,1) = empty

    if condition A then
         Do these things
    end if

    if condition B then
        Do these other things
    end if

    if condition C then
        Do something else
    end if

    lc=lc+1
loop

I would like to go to the top of the loop if any of the conditions are
true.  For example, if condition A is true, I don't want to check for B
or C, just return to the top of the loop.

I thought I could do this with a for/next loop and use the next command
in the if statement but I get an error saying blocked if statement with
out end.  I think exiting the if statement with the next statement left
the if statement{*filter*}.

The real application has approximately 40 of these if statements.  I
have seen it done using one giant if-elseif statement but seems there
should be a cleaner way of doing it.

thanks,

Randy

Sent via Deja.com
http://www.*-*-*.com/



Wed, 11 Jun 2003 03:58:53 GMT  
 How to exit if statement and not continue with Do Loop
This does what you want

Sub test()
If Range("A1") = 1 Then
    MsgBox "A1 = 1"
    GoTo lblEndOfChecks
End If
If Range("A1") = 2 Then
    MsgBox "A1 = 2"
    GoTo lblEndOfChecks
End If
If Range("A1") = 3 Then
    MsgBox "A1 = 3"
    GoTo lblEndOfChecks
End If
lblEndOfChecks:
    MsgBox "End of Checks"
End Sub

Quote:

> Hello!

> Have a routine that looks similar to this:

> Sub test_routine

> lc = 1

> do until cells(lc,1) = empty

>     if condition A then
>          Do these things
>     end if

>     if condition B then
>         Do these other things
>     end if

>     if condition C then
>         Do something else
>     end if

>     lc=lc+1
> loop

> I would like to go to the top of the loop if any of the conditions are
> true.  For example, if condition A is true, I don't want to check for B
> or C, just return to the top of the loop.

> I thought I could do this with a for/next loop and use the next command
> in the if statement but I get an error saying blocked if statement with
> out end.  I think exiting the if statement with the next statement left
> the if statement{*filter*}.

> The real application has approximately 40 of these if statements.  I
> have seen it done using one giant if-elseif statement but seems there
> should be a cleaner way of doing it.

> thanks,

> Randy

> Sent via Deja.com
> http://www.*-*-*.com/



Wed, 11 Jun 2003 04:50:21 GMT  
 How to exit if statement and not continue with Do Loop

You might try

If condition A Then
        Do these things
ElseIf condition B Then
        Do these other things
ElseIf condition C Then
        Do something else
End If

This might be the "giant if-elseif statement" you were referring to, but
with forty different conditions to check, and forty different "do
somethings" to do depending on which of the forty conditions is first true,
I don't see right off how to avoid 80 lines of code. Perhaps someone else
will.

Alan Beban

Quote:

> Hello!

> Have a routine that looks similar to this:

> Sub test_routine

> lc = 1

> do until cells(lc,1) = empty

>     if condition A then
>          Do these things
>     end if

>     if condition B then
>         Do these other things
>     end if

>     if condition C then
>         Do something else
>     end if

>     lc=lc+1
> loop

> I would like to go to the top of the loop if any of the conditions are
> true.  For example, if condition A is true, I don't want to check for B
> or C, just return to the top of the loop.

> I thought I could do this with a for/next loop and use the next command
> in the if statement but I get an error saying blocked if statement with
> out end.  I think exiting the if statement with the next statement left
> the if statement{*filter*}.

> The real application has approximately 40 of these if statements.  I
> have seen it done using one giant if-elseif statement but seems there
> should be a cleaner way of doing it.

> thanks,

> Randy

> Sent via Deja.com
> http://www.*-*-*.com/



Wed, 11 Jun 2003 05:27:17 GMT  
 How to exit if statement and not continue with Do Loop
Randy,

Have you looked up the help on "Select Case..."? This could help if you have
to test a single variable for various values. Otherwise I would suggest
adding a label on the last line within your loop and jump to that line
whenever a condition is met.
  Do Until...
    If condition A Then
      do these things
      goto nxtLoop
    EndIf
    ...
    ...
nxtLoop:
  loop

Regards,
Ton Teuns

Quote:

>Hello!

>Have a routine that looks similar to this:

>Sub test_routine

>lc = 1

>do until cells(lc,1) = empty

>    if condition A then
>         Do these things
>    end if

>    if condition B then
>        Do these other things
>    end if

>    if condition C then
>        Do something else
>    end if

>    lc=lc+1
>loop

>I would like to go to the top of the loop if any of the conditions are
>true.  For example, if condition A is true, I don't want to check for B
>or C, just return to the top of the loop.

>I thought I could do this with a for/next loop and use the next command
>in the if statement but I get an error saying blocked if statement with
>out end.  I think exiting the if statement with the next statement left
>the if statement{*filter*}.

>The real application has approximately 40 of these if statements.  I
>have seen it done using one giant if-elseif statement but seems there
>should be a cleaner way of doing it.

>thanks,

>Randy

>Sent via Deja.com
> http://www.*-*-*.com/



Thu, 12 Jun 2003 07:05:40 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Loop Statement not working

2. Can this be done - continued

3. Outlook 98 Continues to Run After Exiting

4. Outlook 2000 continues to run after exit

5. Outlook.exe Continues to Run After Exit

6. Program continues in background after exiting

7. Outlook2002 continues to check/send after exit

8. why Outlook continue running after exit

9. outlook process continues after exiting

10. how to continue next loop iteration?


 
Powered by phpBB® Forum Software © phpBB Group