VBA Sleep Doesn't Work

后端 未结 7 1295
礼貌的吻别
礼貌的吻别 2020-12-03 10:57

I know I\'m doing something wrong here. I\'m trying to use the sleep function to delay my code, but I get \"Sub or Function not defined\" error. Any tips?

7条回答
  •  失恋的感觉
    2020-12-03 11:33

    With this code Excel not freeze and the CPU usage is low:

    Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Sub Delay(s As Single)
        Dim TimeOut As Single
        TimeOut = Timer + s
        Do While Timer < TimeOut
            DoEvents
            Sleep 1 'With this line the CPU usage is 00 instead of 50 with an absolute error of +1ms and the latency of 1ms.
        Loop
    End Sub
    

提交回复
热议问题