VBA开发中经常需要提示消息框,如果不关闭程序就会暂时中断,这里分享下VBA如何实现消息框的自动关闭,总共有三种方法:
第一种方法
Public Declare Function MsgBoxTimeOut Lib "user32" Alias "MessageBoxTimeoutA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long, ByVal wlange As Long, ByVal dwTimeout As Long) As LongPublic Sub 录入对话() '过程,"弹出对话","对话框标题",图标类型,默认参数,N秒后自动关闭MsgBoxTimeOut 0, "录入完毕!!", "提示", 64, 0, 1500End Sub
第二种方法
Sub pop()Dim wsh As ObjectSet wsh = CreateObject("wscript.shell")wsh.popup "请您输入有效数字", 1, "注意", vbInformation'CreateObject ("wscript.shell").popup "请您输入有效数字", 1, "注意", vbInformationEnd Sub
第三种方法
Public Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nidevent As Long, ByVal uelaspe As Long, ByVal lptimerfunc As Long) As LongPublic Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nidevent As Long) As LongDim TID As LongConst sec = 3Sub closetest(ByVal hwnd As Long, ByVal umsg As Long, ByVal idevent As Long, ByVal systime As Long)Application.SendKeys "~", TrueKillTimer 0, TIDEnd SubSub 三秒钟后关闭()TID = SetTimer(0, 0, sec * 1000, AddressOf closetest)MsgBox sec & "秒钟自动关闭窗口", 65, "提示"End Sub