当我使用Sleep时,如何防止后台宏停止?

时间:2021-09-01 21:03:22

Whenever I run Sleep, all my background macros stop working. i want to be able to do OnKey events while having the sleep constantly running.

每当我运行Sleep时,我的所有背景宏都会停止工作。我希望能够在睡眠不断运行的同时做OnKey事件。

Here's the part with sleep API

这是睡眠API的一部分

Sub Bombs()
 Do While bomb.Offset(, 1).Value <> "*"
    bomb.clear
    Set bomb = bomb.Offset(, 1)
    bomb.Interior.Color = vbRed
    Sleep (200)
Loop
End Sub

it basically causes a red cell to move accross the screen.

它基本上导致红色单元格在屏幕上移动。

1 个解决方案

#1


6  

I use a customized sub Wait() that I created some years ago which I usually use in lieu of Sleep. See if it helps you. Here is a very basic example that I created for testing purpose.

我使用了几年前创建的定制子Wait(),我通常用它来代替Sleep。看看它是否对你有所帮助。这是我为测试目的而创建的一个非常基本的示例。

Sub Bombs()
    Application.OnKey "{DOWN}", "Sample"

    Dim bomb As Range

    Set bomb = ActiveCell

    Do While bomb.Offset(, 1).Value <> "*"
        bomb.Clear
        Set bomb = bomb.Offset(, 1)
        bomb.Interior.Color = vbRed
        Wait 5
    Loop

    Application.OnKey "{DOWN}", ""
End Sub

Private Sub Wait(ByVal nSec As Long)
    nSec = nSec + Timer
    While nSec > Timer
        DoEvents
    Wend
End Sub

#1


6  

I use a customized sub Wait() that I created some years ago which I usually use in lieu of Sleep. See if it helps you. Here is a very basic example that I created for testing purpose.

我使用了几年前创建的定制子Wait(),我通常用它来代替Sleep。看看它是否对你有所帮助。这是我为测试目的而创建的一个非常基本的示例。

Sub Bombs()
    Application.OnKey "{DOWN}", "Sample"

    Dim bomb As Range

    Set bomb = ActiveCell

    Do While bomb.Offset(, 1).Value <> "*"
        bomb.Clear
        Set bomb = bomb.Offset(, 1)
        bomb.Interior.Color = vbRed
        Wait 5
    Loop

    Application.OnKey "{DOWN}", ""
End Sub

Private Sub Wait(ByVal nSec As Long)
    nSec = nSec + Timer
    While nSec > Timer
        DoEvents
    Wend
End Sub