Kris2
05-15-2008, 07:06 AM
Hello,
Just like NoHomework81 (http://www.xtremevbtalk.com/showthread.php?t=145849), i'm working on a large VBA module and wanted to use the GoTo for jumping out of a sub (which wasnt possible).
Do until ...
Do until INTcar = INTallcars
Call SetValues
Call Entrance
Call Accelerate
Call ...
end:
INTcar = INTcar + 1
Loop
INTcurrenttime = INTcurrenttime + 1
Loop
-------------------------------------
Sub Entrance ()
If INTstarttime > INTcurrenttime Then GoTo end
(code...)
End Sub
Is there another way to exit the current sub and skip some other subs?
Thanks!
Kris
A need for GoTo is almost always a sign of a flawed design. Perhaps you can explain in more detail what you need to do here, and we can offer a better approach?
Kris2
05-15-2008, 08:08 AM
The code is a piece of a simulation.
When a car participates in this simulation, it's INTstarttime needs to be the same or more as the INTcurrenttime. So when the INTstarttime is > then the INTcurrenttime, i need to jump to the next car.
So I'm trying to jump from within the sub to the end of the loop.
geodekl
05-15-2008, 09:38 AM
Sub demo()
Dim intA% 'declare variable as integer
Do Until intA >= 5 'start to loop (also see "do while", "do while not")
'when intA >= 5 the loop will be finished
intA = intA + 1 'do stuff
Loop 'keep looping
Do Until intA >= 50
intA = intA + 1
If intA = 7 Then Exit Do 'get out of the loop early if a condition is met
'also see "exit for"
Loop
End Sub
If this doesn't get you closer to what you're trying to do, post again with more details (and an example) of what you're attempting to accomplish.
The easiest way to facilitate this is to return a boolean value from the procedure that determines whether to run the others or not. I.e. something like this:
Do until ...
Do until INTcar = INTallcars
Call SetValues
If Entrance() Then
Call Accelerate
Call ...
End If
INTcar = INTcar + 1
Loop
INTcurrenttime = INTcurrenttime + 1
Loop
-------------------------------------
Function Entrance () As Boolean
If INTstarttime > INTcurrenttime Then
Entrance = False
Else
'(code...)
Entrance = True
End If
End Sub
If the other subs should also be able to signal skipping the rest of the chain, you simply modify them accordingly and nest the if-blocks:
If Entrance() Then
If Accelerate() Then
'...
End If
End If
Kris2
05-15-2008, 10:00 AM
Thanks for the comments Lebb, Geodek and especially Cas!
I's working fine now.
Kris