#If VBA7 Then
' 64位系统声明
Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#Else
' 32位系统声明
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If
Sub PauseForOneMillisecond()
' 暂停1毫秒
Sleep 3000
MsgBox "ok"
End Sub
经验证,在多重循环语或大量循环任务情况下, doevents可预防电脑死机
当需要在程序中暂停一定时间时,可采用以下代码:
excel vba 可以,cad vba不支持application.wait
Sub a()
Application.Wait (Now + TimeValue("0:00:02"))
End Sub
或调用api:
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Integer)
Sub a()
Sleep 900
End Sub
或
time1 = Timer
Do While dt < 2
dt = Timer - time1
i = i + 1
Loop
查看 电脑一秒钟运行多少此的代码:
Dim time1 As Double, dt As Double
time1 = Timer
dt = 0
Do While dt < 1
dt = Timer - time1
i = i + 1
Loop
DoEvents
MsgBox i
Stop