vba Wscript.sleep 1000 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41647084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 11:53:12  来源:igfitidea点击:

Wscript.sleep 1000 not working

vbaexcel-vbawshexcel

提问by dee

I have the following VBA code:

我有以下 VBA 代码:

Set wshshell = CreateObject("WScript.Shell")
wshshell.SendKeys "{F3}"
WScript.Sleep 1000

I get the following error on the WSCript.Sleepline:

我在线上收到以下错误WSCript.Sleep

Run time error 424: Object required

运行时错误 424:需要对象

回答by cyboashu

WScript.Sleeponly runs when used inside scripting host controls. VBA doesn't support it. You can, however use the following example to delay.

WScript.Sleep仅在脚本宿主控件中使用时运行。VBA 不支持它。但是,您可以使用以下示例进行延迟。

  #If VBA7 Then
        Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    #Else
        Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    #End If
    Sub Test()

        MsgBox "one"
        Sleep 1000
        MsgBox "Two"
    End Sub