Hello,
Do any of you know a solution for the following:-
If you have an application.ontime statement that calls another procedure, and then the next statement in the same sub is calling a procedure, VBA ignores the application.ontime delay and starts running the next statement before the application.ontime statement has finished. I have some example code below to illustrate this.
So in this example when I run macro1, VBA starts macro3 before macro 2 has finished. So effectively the sequence is macro1,macro3,macro2 rather than the desired macro1, 5 second delay and then macro2, macro3. Effectively VBA runs macro 3 whilst the 5 second delay is running and then runs macro2
Now I know I could call macro3 from macro2. And that would give me the desired order, but for more sophisticated structured programs this is not an acceptable solution. I have tried using application.enableevents thinking this might be the solution but to no success so far.
Please can anyone help?
Do any of you know a solution for the following:-
If you have an application.ontime statement that calls another procedure, and then the next statement in the same sub is calling a procedure, VBA ignores the application.ontime delay and starts running the next statement before the application.ontime statement has finished. I have some example code below to illustrate this.
So in this example when I run macro1, VBA starts macro3 before macro 2 has finished. So effectively the sequence is macro1,macro3,macro2 rather than the desired macro1, 5 second delay and then macro2, macro3. Effectively VBA runs macro 3 whilst the 5 second delay is running and then runs macro2
Now I know I could call macro3 from macro2. And that would give me the desired order, but for more sophisticated structured programs this is not an acceptable solution. I have tried using application.enableevents thinking this might be the solution but to no success so far.
Please can anyone help?
Code:
Sub macro1()
Dim n As Integer
n = n + 1
Application.OnTime Now + TimeValue("00:00:05"), "macro2"
Call macro3
End Sub
Sub macro2()
Dim n As Integer
n = n + 1
MsgBox ("macro2")
End Sub
Sub macro3()
Dim n As Integer
n = n + 1
MsgBox ("Macro 3")
End Sub