有时需要程序暂停运行,如等待Excel单元格中的函数计算完毕。等待的方式有两种。
一、DoEvents
'……前面的代码……
Timer0 = Timer
Do Until Timer - Timer0 > 1 '此处设定为等待1秒
DoEvents
Loop
'……后续的代码……
二、API函数Sleep
先在模块首段声明:
如果是32位Office,则声明:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
如果是64位Office,则声明:
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
然后再在某个Sub或Function中加入:
'……前面的代码……
Sleep 1000 '延时1000毫秒
'……后续的代码……
网友评论