exit sub routine to goto next loop

SQUIDD

Well-known Member
Joined
Jan 2, 2009
Messages
2,104
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hi All

So im wondering how best to tackle this.

Im checking through data and i an checking if scenarios are met.

for example, a very simplified example below.

in fact while writing the example i had a little idea about declaring a global flag status of some description, then if the flag is activate, simple check this at the top of all the check macros, and simply use exit sub

but i still wonder if there is another or better way.

i hope this makes sense, but ultimatly if any of the check subs are unsatisfied, we literally skip to the next A in sub TT

thanks for looking, oh yes, i do not want to write it into 1 sub either and use goto, i have far too many checks to write it that long and confusing.

Dave

VBA Code:
Sub TT()
For a = 1 To 100
    If b = 1 Then Call check1
    If c = 1 Then Call check2
    If d = 1 Then Call check3
    If e = 1 Then Call check4
    If f = 1 Then Call check5
    'skip to here if checks not satisfied
Next a
End Sub
Sub check1()
If f = 6 Then 'at this point exit the sub, but also do not action any more of the calls, so next A in sub TT
End Sub
Sub check2()
If f = 6 Then 'at this point exit the sub, but also do not action any more of the calls, so next A in sub TT
End Sub
Sub check3()
If f = 6 Then 'at this point exit the sub, but also do not action any more of the calls, so next A in sub TT
End Sub
Sub check4()
If f = 6 Then 'at this point exit the sub, but also do not action any more of the calls, so next A in sub TT
End Sub
Sub check5()
If f = 6 Then 'at this point exit the sub, but also do not action any more of the calls, so next A in sub TT
End Sub
Sub check6()
If f = 6 Then 'at this point exit the sub, but also do not action any more of the calls, so next A in sub TT
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
ok, well setting a flag status worked really well, so posted it as example, i am happy with it

VBA Code:
PUBLIC FLAG As Integer    
Sub TT()
For a = 1 To 100
FLAG = 0
If b = 1 Then Call check1
If c = 1 Then Call check2
If d = 1 Then Call check3
If e = 1 Then Call check4
If f = 1 Then Call check5
'skip to here if checks not satisfied
Next a
End Sub
Sub check1()
IF FLAG = 1 THEN EXIT SUB
If f = 6 Then FLAG =1:EXIT SUB
End Sub
Sub check2()
IF FLAG = 1 THEN EXIT SUB
If f = 6 Then FLAG =1:EXIT SUB
End Sub
Sub check3()
IF FLAG = 1 THEN EXIT SUB
If f = 6 Then FLAG =1:EXIT SUB
End Sub
Sub check4()
IF FLAG = 1 THEN EXIT SUB
If f = 6 Then FLAG =1:EXIT SUB
End Sub
Sub check5()
IF FLAG = 1 THEN EXIT SUB
If f = 6 Then FLAG =1:EXIT SUB
End Sub
Sub check6()
IF FLAG = 1 THEN EXIT SUB
If f = 6 Then FLAG =1:EXIT SUB
End Sub
 
Upvote 0
Yeah, that works, but it is not good software design. It uses common environment coupling, an undesirable type of coupling. I would recommend this more structure approach:

VBA Code:
Sub TT()

   Dim SubFailure As Boolean

   For a = 1 To 100
   
       SubFailure = False
       
       If b = 1 And Not SubFailure Then check1 SubFailure
       If c = 1 And Not SubFailure Then check2 SubFailure
       If d = 1 And Not SubFailure Then check3 SubFailure
       If e = 1 And Not SubFailure Then check4 SubFailure
       If f = 1 And Not SubFailure Then check5 SubFailure
       'skip to here if checks not satisfied
       
   Next a

End Sub
Sub check1(SubFailure As Boolean)
   If f = 6 Then
      SubFailure = True   'at this point exit the sub, but also do not action any more of the calls, so next A in sub TT
   Else
      ' do your other stuff here
   End If
End Sub
Sub check2(SubFailure As Boolean)
   If f = 6 Then
      SubFailure = True   'at this point exit the sub, but also do not action any more of the calls, so next A in sub TT
   Else
      ' do your other stuff here
   End If
End Sub
Sub check3(SubFailure As Boolean)
   If f = 6 Then
      SubFailure = True   'at this point exit the sub, but also do not action any more of the calls, so next A in sub TT
   Else
      ' do your other stuff here
   End If
End Sub
Sub check4(SubFailure As Boolean)
   If f = 6 Then
      SubFailure = True   'at this point exit the sub, but also do not action any more of the calls, so next A in sub TT
   Else
      ' do your other stuff here
   End If
End Sub
Sub check5(SubFailure As Boolean)
   If f = 6 Then
      SubFailure = True   'at this point exit the sub, but also do not action any more of the calls, so next A in sub TT
   Else
      ' do your other stuff here
   End If
End Sub
Sub check6(SubFailure As Boolean)
   If f = 6 Then
      SubFailure = True   'at this point exit the sub, but also do not action any more of the calls, so next A in sub TT
   Else
      ' do your other stuff here
   End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,453
Members
448,967
Latest member
grijken

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top