MsgBoxEx函数能满足你的要求,把DebugPrint 改成你想执行的语句即可
wType参数改成可以提示输入VBA的vbMsgboxStyle常数。
这个API函数的参数如下:
hwnd:窗口句柄,可以设为0
lpText:消息框显示内容,类似于MsgBox函数的第一个参数Prompt
lpCaption:消息框标题,类似于MsgBox函数的第三个参数Caption
wType:消息框类型,类似于MsgBox函数的第二个参数Buttons
wlange:不是太明白这个参数,0或者1都看不出什么差别
dwTimeout:延时时间,单位是毫秒
返回的值和vbMsgBoxResult常数一样,多了一个返回值32000表示超过延时时间未选择任何按钮。
Private Declare Function MsgBoxEx Lib "user32" Alias "MessageBoxTimeoutA" ( _
ByVal hwnd As Long, _
ByVal lpText As String, _
ByVal lpCaption As String, _
ByVal wType As VbMsgBoxStyle, _
ByVal wlange As Long, _
ByVal dwTimeout As Long) As Long‘函数声明
Private Sub TestMsgboxEx()’使用
Dim ret As Long
ret = MsgBoxEx(0, "要终止此程序么", "60秒后自动关闭", vbYesNo + vbInformation, 1, 60000)
If ret = 32000 Or ret = vbYes Then End
End Sub
Public n As Integer
Sub test()
If n < 100 Then
ApplicationOnTime Now + TimeValue("0:0:5"), "test"
'DebugPrint n
n = n + 1
End If
End Sub
VB中有一个API函数叫Sleep可以实现暂停。或者用下面的自定义过程waitsec来实现暂停。
Sub mmm()
MsgBox "XX"
waitsec 2
MsgBox "XX"
waitsec 2
MsgBox "XX"
End Sub
Private Sub waitsec(ByVal dS As Double)
Dim sTimer As Date
sTimer = Timer
Do
DoEvents
Loop While Format((Timer - sTimer), "000") < dS
End Sub
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim sf As Boolean
Private Sub CommandButton1_Click()
Dim i As Integer
sf = True
Do While True
DoEvents
Sleep 500
If sf = True Then
Sheet1[a1] = Int(Rnd 100)
Else
Exit Sub
End If
Loop
End Sub
Private Sub CommandButton2_Click()
sf = False
End Sub
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwmilliseconds As Long)
Private Sub 输出去重数字_Click()
ApplicationScreenUpdating = False
ApplicationCalculation = xlCalculationManual
Dim sh1 As Worksheet, i As Long
Set sh1 = ThisWorkbookSheets(1)
rr1 = sh1[a1000000]End(xlUp)row
i = 2
Do While sh1Cells(i, 1) <> ""
sh1Cells(i, 10) = ApplicationCountIf(sh1Range("A2:A" & rr1), Cells(i, 6))
i = i + 1
If i / 3000 = Int(i / 3000) Then Sleep 2000
Loop
MsgBox "输出完毕!"
End Sub
以上就是关于Excel中VB编程如何实现延时等待全部的内容,包括:Excel中VB编程如何实现延时等待、让vba每隔五秒钟运行一次共一百次、excel vba中如何实现时间等待等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)