DoEvents, Waiting, and Editing

别等时光非礼了梦想. 提交于 2019-12-04 09:18:49
Doug Glancy

Instead of Wait, try OnTime. To demonstrate, paste this in a normal module and run Test. Range A1 of the active sheet will increment every five seconds and you'll be able to work in between. It also works if you are in Edit mode when the five seconds elapses:

Sub test()
    test2
End Sub

Sub test2()
    ActiveSheet.Cells(1, 1).Value = ActiveSheet.Cells(1, 1).Value + 1
    Application.OnTime Now + TimeValue("00:00:5"), "test2"
End Sub

Note that the OnTime statement at the end of the sub calls the sub again recursively. Here's some more info.

Sub mySub()
    Do
        If Time() >= #3:00:00 AM# And Time() <= #7:00:00 AM# Then
            Aespire_Production_Board
        End If
    DoEvents
    Loop
End Sub

Once you start mySub() it will run indefinately. Between 3 AM and 7 AM it will run Aespire_Production_Board on every loop. It also allows the user to interact. The code can be put into Break mode using CTRL-Break.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!