Question

I don't understand why I have this row Application.Wait in error (highlighted in yellow) ?

Sub Wait_Random()
Dim Low As Double
Dim High As Double
Low = 90 '<<< CHANGE AS DESIRED
High = 126 '<<< CHANGE AS DESIRED
r = Int((High - Low + 1) * Rnd() + Low)
Application.Wait Now + TimeValue("00:00:" & CStr(r))
End Sub

I have found that the problem is that this code is trying "to add a number to a string of characters". But I am lost ! Thanks in advance ;)

Was it helpful?

Solution

It's because your r variable always greater than 59 sec. (actually it's alwasys greater 90). You should change Low and High so that r would never be greater 59:

Sub Wait_Random()
    Dim Low As Double
    Dim High As Double
    Dim r as Integer
    Low = 1 '<<< CHANGE AS DESIRED
    High = 59 '<<< CHANGE AS DESIRED
    r = Int((High - Low) * Rnd() + Low)
    Application.Wait Now + TimeValue("00:00:" & CStr(r))
End Sub

Or another way you could use

Application.Wait DateAdd("s", r, Now)

to add r seconds. In that case r could be greater 59.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top