vba - 当我使用 Sleep 时如何防止我的后台宏停止?

标签 vba excel

每当我运行 Sleep 时,我所有的后台宏都会停止工作。我希望能够在 sleep 不断运行的同时执行 OnKey 事件。

这是 sleep 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

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

最佳答案

我使用几年前创建的自定义子 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

关于vba - 当我使用 Sleep 时如何防止我的后台宏停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21588696/

相关文章:

excel - 当 'On Error Resume Next' 将先前的值留在变量中时,如何返回空白或零?

excel - 将图表导出为图像 - 只需单击按钮

sql - 查表找到某个ID : error 80040e10 no value for one or more parameters

VBA - "Wrong number of arguments or invalid property assignment"- 单元格()

c# - EPPlus 添加带有最新版本 Office Losses All 宏的工作表

c# - Range[] 而不是 get_Range()

Excel工作日计算

vba - .Cells(.Rows.Count ,"A").End(xlUp).row 的含义

excel - 在 VBA 中从范围底部向上执行 FIND

vba - 使用 VBA 直接将一系列数字条目转换为文本