I have a set of code that contains:
Application.Wait (Now + TimeValue("4:00:00"))
This is essentially pausing the macro for a four hour window from 3 AM (when it finishs running the code) till 7 AM (when it should resume). The code is on an endless loop essentially.
I want the user to be able to have control during that time to edit certain cells. I have tried
DoEvents
but have not found the way to keep the macro running, yet provide control to the user during that time when the macro is doing nothing but waiting.
Any insight would be appreciated. Thanks!
EDIT:
One more followup question. I created this macro to reference the actual macro "Production_Board". I want this macro to run all the time and refresh as often as possible. By using the goto startagain, it tries to start to launch the macro again before the macro has even started due to the "ontime" delay interval.
How could I make the sub RunMacro start again the second that the macro "Production_Board" finishes?
Sub RunMacro
startagain:
Dim hour As Integer
Dim OT As String
hour = 0
OT = "Empty"
hour = Sheets("Calculations").Range("DR1").Value
OT = Sheets("Black").Range("D4").Value
If OT = "Y" Then
If hour = 3 Or hour = 4 Then
Application.OnTime TimeValue("05:00:00"), "Aespire_Production_Board"
Else
Application.OnTime Now + TimeValue("00:00:30"), "Aespire_Production_Board"
End If
Else
If hour = 3 Or hour = 4 Or hour = 5 Or hour = 6 Then
Application.OnTime TimeValue("07:00:00"), "Aespire_Production_Board"
Else
Application.OnTime Now + TimeValue("00:00:30"), "Aespire_Production_Board"
End If
DoEvents
GoTo startagain
DoEvents is most useful for simple things like allowing a user to cancel a process after it has started, for example a search for a file. For long-running processes, yielding the processor is better accomplished by using a Timer or delegating the task to an ActiveX EXE component.
Use Sleep Function in VBA And then you need to make sure to append the “PtrSafe” statement if you are using 64 Bit Excel. Next, you need to call the sleep function in the code. In the end, specify the time (milliseconds) for which you want to delay the code.
Excel VBA DoEvents – Example #2Step 1: Write the subprocedure of VBA DoEvents as shown below. Step 2: Directly open a For-next loop without defining a variable. Step 3: Choose a number range we will see running. Let the range be 1 to 20000 same as example-1.
VBA Wait is a built-in function to pause the code from executing for a specified time. It is very similar to what we do in the Sleep command. To pause a code, we use the Application. Wait method.
Instead of Wait
, try OnTime
. To demonstrate, paste this in a normal module and run Test
. Range A1 of the active sheet will increment every five seconds and you'll be able to work in between. It also works if you are in Edit mode when the five seconds elapses:
Sub test()
test2
End Sub
Sub test2()
ActiveSheet.Cells(1, 1).Value = ActiveSheet.Cells(1, 1).Value + 1
Application.OnTime Now + TimeValue("00:00:5"), "test2"
End Sub
Note that the OnTime
statement at the end of the sub calls the sub again recursively. Here's some more info.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With