Temporarily disable macro for 5 seconds and then have it continue

eques247

New Member
Joined
Oct 22, 2015
Messages
7
Hi,

I'm trying to figure out how to temporarily disable my macro for like 5 seconds and then have it continue running the rest of the script. I tried using the OnTime function (shown below), but it caused the following error message to pop up "Cannot run the macro 'C:\...\testbook.xlsm'!statuscheck'. The macro may not be available in this workbook or all macros may be disabled."

Code:
Sub test()
Dim i As Integer

For i = 1 To 3
Application.Calculate
Next i

Application.OnTime Now + TimeValue("00:00:05"), "statuscheck"

End sub

Sub statuscheck()

Msgbox "done"

End sub

Any help with this would be much appreciated. Thanks!
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
You don't need the time delay. Just insert the macro call into the code you are running at the point you want to run Statuscheck.
Code:
Sub t()
MsgBox "Macro is being called"
Statuscheck
MsgBox "Continue this macro"
End Sub

Just make sure that Statuscheck is in the standard code module 1.
 
Upvote 0
Thanks for your reply. The reason why I need the time delay is that when the calculate command is run, it is supposed to start importing data from Bloomberg into my excel workbook. Unfortunately, the data only gets imported when no macro is running, which is why I need some kind of break function and then have it continue after a few seconds. With my current code, the msgbox "Done" is displayed prematurely before the data is actually fully imported
 
Upvote 0
Thanks for your reply. The reason why I need the time delay is that when the calculate command is run, it is supposed to start importing data from Bloomberg into my excel workbook. Unfortunately, the data only gets imported when no macro is running, which is why I need some kind of break function and then have it continue after a few seconds. With my current code, the msgbox "Done" is displayed prematurely before the data is actually fully imported

You can try this snippet. It will do a 5 second pause and let other activity take place simultaneously. I would suggest putting it immediately after the macro call.
Code:
s = Timer + 5
Do While Timer < s
	DoEvents
Loop
If you need more or less time, you just change the 5 to whatever you need. You can use decimal places for tenths and hundredths of a second, if needed.
 
Last edited:
Upvote 0
If you want to set it up like a function call.
Code:
Sub TimeDelay(amt As Double)
s = Timer + amt
Do While Timer < s
    DoEvents
Loop
End Sub

Then to use it in your code
Code:
Sub t()
Statuscheck
TimeDelay 5
MsgBox "OK"
End Sub

The delay code would go in the standard code module 1.
 
Last edited:
Upvote 0
You can also use the following:

Application.Wait (Now + TimeValue("00:00:05")).

This forces your macro to wait 5 seconds, before it continues with the next line of code.
 
Upvote 0
You can also use the following:

Application.Wait (Now + TimeValue("00:00:05")).

This forces your macro to wait 5 seconds, before it continues with the next line of code.

For some reason this didn't work either. I guess the data can only be imported when no macro is running which is quite counterintuitive. Are there any other options I could utilize? Like somehow exit the sub and then reenter it after a few seconds
 
Upvote 0
You can try this snippet. It will do a 5 second pause and let other activity take place simultaneously. I would suggest putting it immediately after the macro call.
Code:
s = Timer + 5
Do While Timer < s
    DoEvents
Loop
If you need more or less time, you just change the 5 to whatever you need. You can use decimal places for tenths and hundredths of a second, if needed.
Thanks!! I'll try this out when I get back to my Bloomberg machine on Monday. Sadly, I do not have remote access to Bloomberg.
 
Upvote 0
Hi all. I was able to figure it out and get it to work. My issue was that I was missing a special Bloomberg API function

Code:
Do while Left(Range("[COLOR=#0000cd]first cell in the data table[/COLOR]"),3) = "Ret"
     BloombergUI.RefreshAllStaticData
     DoEvents
Loop

Thanks everyone for your help!
 
Upvote 0
Hi all. I was able to figure it out and get it to work. My issue was that I was missing a special Bloomberg API function

Code:
Do while Left(Range("[COLOR=#0000cd]first cell in the data table[/COLOR]"),3) = "Ret"
     BloombergUI.RefreshAllStaticData
     DoEvents
Loop

Thanks everyone for your help!
Thanks for the feedback,
Regards, JLG
 
Upvote 0

Forum statistics

Threads
1,216,523
Messages
6,131,171
Members
449,627
Latest member
ChrisNoMates

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