VBA精彩代码分享-2
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 Long Public Sub 录入对话() '过程,"弹出对话","对话框标题",图标类型,默认参数,N秒后自动关闭 MsgBoxTimeOut 0, "录入完毕!!", "提示", 64, 0, 1500 End Sub
第二种方法
Sub pop() Dim wsh As Object Set wsh = CreateObject("wscript.shell") wsh.popup "请您输入有效数字", 1, "注意", vbInformation 'CreateObject ("wscript.shell").popup "请您输入有效数字", 1, "注意", vbInformation End Sub
第三种方法
Public Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nidevent As Long, ByVal uelaspe As Long, ByVal lptimerfunc As Long) As Long Public Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nidevent As Long) As Long Dim TID As Long Const sec = 3 Sub closetest(ByVal hwnd As Long, ByVal umsg As Long, ByVal idevent As Long, ByVal systime As Long) Application.SendKeys "~", True KillTimer 0, TID End Sub Sub 三秒钟后关闭() TID = SetTimer(0, 0, sec * 1000, AddressOf closetest) MsgBox sec & "秒钟自动关闭窗口", 65, "提示" End Sub