Random wait time in application.wait VBA

sherafyk

New Member
Joined
Aug 19, 2016
Messages
7
Hello everyone, hoping you could help me out. Currently I have a line of code in a macro: Application.Wait (Now + TimeValue("00:04:00"))

In this example the application will pause for 4 minutes. I was wondering what I would have to do to randomize that time value, say anywhere from 1 to 10 minutes, for example. I believe it has something to do with defining a random variable, and placing that in the time slot, but I am completely lost on how to do this. Any advice would be of great help.

Thanks!
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Currently I have a line of code in a macro:
Application.Wait (Now + TimeValue("00:04:00"))
[....] I was wondering what I would have to do to randomize that time value, say anywhere from 1 to 10 minutes, for example.

Well, you could write:

Application.Wait Now + TimeSerial(0, WorksheetFunction.RandBetween(1,10), 0)

But why would you want to do that?!!

First, even waiting 1 minute is an "eternity", much less 10 minutes.

Second, you cannot do anything with Excel (or VBA) while waiting.

(There are some asynchronous actions that Excel might still be able to perform while waiting.)

An alternative might be:

Application.OnTime Now + TimeSerial(0, WorksheetFunction.RandBetween(1,10), 0), "part2"

where "part2" is another VBA procedure that contains any VBA code that should be executed after the 1-to-10-minute delay.

But it is still unclear why you would want to do even that. And if your reasons are good, there might be issues that prohibit that design and additional issues to consider, for example preventing the OnTime event if you close the workbook before the event occurs.
 
Upvote 0
Hi, thank you so much, this worked perfectly. Long story short, I have a macro that (in the past) would run a query at a set interval (say, every 11.4 minutes). This is run on a separate system whose sole purpose is to run this macro. I wanted to "humanize" this function a bit and give it a more random schedule so my users don't pick up on the time line and try to game the system with an unfair advantage.

Thanks again for all your help!
 
Upvote 0
Hi Seniors,

In the same context I would need code for the the function to randomize the time in seconds and how to insert that function in the main Macro Program. Please help!

Thanks in advance
 
Upvote 0
I was able to randomly select around a time by this:
Dim x As Integer
x = Int((10 - 3 + 1) * Rnd + 5)

Application.Wait (Now + TimeValue("0:00:" + CStr(x)))

Hope that helps. This helped me "huminize" my vba in trying to fix ReCaptcha
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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