Application.Wait和Excel VBA运行时错误'13'类型不匹配

时间:2014-02-22 10:03:19

标签: excel vba excel-vba excel-2003

我不明白为什么我错误地将这行 Application.Wait (以黄色突出显示)?

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

我发现问题是这段代码正在尝试“为一串字符添加一个数字”。但我迷路了!在此先感谢;)

1 个答案:

答案 0 :(得分:1)

这是因为您的r变量总是大于59秒。 (实际上它的alwasys更大90)。您应该更改LowHigh,以便r永远不会更大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

或者您可以使用其他方式

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

添加r秒。在这种情况下,r可能更大59