PDA

View Full Version : Events to go to certain parts of code after a value is entered



chacanger
11-19-2012, 04:19 PM
Hello

I've been wondering if there was a simple way of doing this, I have a lot of code were there are multiple procedures being opened within other procedures and multiple bits of looping code. I have made a rough example below of what I mean...

Option Explicit
Dim x As Byte, z As Byte

Sub ProcedureOne()

Do Until x = 4

Do Until z = 3

ProcedureTwo
z = z + 1

Loop

Loop

Msgbox "x = 4"

End Sub

Sub ProcedureTwo()

ProcedureThree

End Sub

Sub ProcedureThree()

x = 4

End Sub



As you can see there are 2 loops and 2 procedures nested within each other, Is there a way once x = 4, rather than having to put an If statement saying when it reaches 4 Exit Sub, then have another If statement on the first procedure when it reaches 4 Exit Do, just have it so that when x does = 4, it goes straight to the part with the Msgbox when it's in ProcedureThree.

Obviously the code I actually have is 100 times longer due to many different things going on, so it's not as simple as this bit of code, but if I can get a rough idea on how to do it, I can do the rest. I get an idea it would be something like an event such as Workbook_WindowActivate were it activates as soon as something happens.

Sorry if this doesn't make sense, I couldn't describe it very easily.

Many Thanks

Chacanger

omp001
11-20-2012, 11:57 AM
Hi. Have you tried this way ?


Sub ProcedureOne()
Do Until x = 4
Do Until z = 3 Or x = 4
ProcedureTwo
z = z + 1
Loop
Loop
MsgBox "x = 4"
x = 0
End Sub

chacanger
11-20-2012, 12:42 PM
Hi. Have you tried this way ?


Sub ProcedureOne()
Do Until x = 4
Do Until z = 3 Or x = 4
ProcedureTwo
z = z + 1
Loop
Loop
MsgBox "x = 4"
x = 0
End Sub


Hello omp001

Thanks for the reply, unfortunately this won't work in my case as my code needs to see if a specific value comes comes up in the code at any point and must go through multiple procedures/functions and loops etc.

I had a think about this and thought of being able to do something like the On Error Goto feature which goes straight to a specific part regardless of what procedure/function it is, so for example if we pretend in this code below that x = x / 0 is supposed to be x = 4, I would want my code to go straight to the bit with foundvalue.

Sub ProcedureOne()

On Error GoTo foundvalue

Do Until x = 4
Do Until z = 3 Or x = 4
ProcedureTwo
z = z + 1
Loop
Loop
x = 0

Exit Sub

foundvalue:

MsgBox "x = 4"

End Sub

Sub ProcedureTwo()

ProcedureThree

End Sub

Sub ProcedureThree()

x = x / 0 'Say this is x = 4

End Sub


Cheers

Chacanger