Uploaded January 2020 | Updated September 2026, 2 weeks ago
How to create a message box automatically that closes itself after a specific time using VBA.
Here's the complete VBA code:
Private Sub Workbook_Open()
startTimer
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
startTimer
End Sub
Option Explicit
Const PopupDurationSecs As Integer = 5
Sub startTimer()
Application.OnTime Now + TimeValue("00:00:05"), "myShellMessageBox"
End Sub
Sub myShellMessageBox()
Dim Result As Integer
Result = CreateObject("WScript.Shell").PopUp( _
"Keep this workbook open?", PopupDurationSecs, _
"Keep Workbook Open", 4 + 32)
'MsgBox Result
If Result = 6 Then
Exit Sub
ElseIf Result = 7 Then
'ThisWorkbook.Save
Application.Quit
End If
End Sub
For more on this method see the MSDN documentation at msdn.microsoft.com/en-us/library/x83z1d9f%28v=VS.85%29.aspx
How to create a message box automatically that closes itself after a specific time using VBA.
Here's the complete VBA code:
Private Sub Workbook_Open()
startTimer
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
startTimer
End Sub
Option Explicit
Const PopupDurationSecs As Integer = 5
Sub startTimer()
Application.OnTime Now + TimeValue("00:00:05"), "myShellMessageBox"
End Sub
Sub myShellMessageBox()
Dim Result As Integer
Result = CreateObject("WScript.Shell").PopUp( _
"Keep this workbook open?", PopupDurationSecs, _
"Keep Workbook Open", 4 + 32)
'MsgBox Result
If Result = 6 Then
Exit Sub
ElseIf Result = 7 Then
'ThisWorkbook.Save
Application.Quit
End If
End Sub
For more on this method see the MSDN documentation at msdn.microsoft.com/en-us/library/x83z1d9f%28v=VS.85%29.aspx

![Using ADO and SQL with VBA
How to use ADO and SQL with VBA to analyze large Excel worksheet data automatically.
Heres the complete VBA code:
Sub sbADOExample()
Dim sSQLQry As String
Dim Conn As New ADODB.Connection
Dim myrs As New ADODB.Recordset
Dim sconnect As String
sconnect = Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ= _
& ActiveWorkbook.Path & Application.PathSeparator & ActiveWorkbook.Name
Conn.Open sconnect
Dim VehicleModel As String, Region As String, CustomerType As String
VehicleModel = InputBox(Enter a vehicle model)
Table Name = Sheet Name = [Sheet1$]
sSQLQry = SELECT * From [Sheet1$] WHERE
If VehicleModel NOT EQUAL TO Then
sSQLQry = sSQLQry & [Vehicle Model]= & VehicleModel &
End If
Region = InputBox(Enter a region name)
If Region NOT EQUAL TO Then
sSQLQry = sSQLQry & AND[Region]= & Region &
End If
CustomerType = InputBox(Enter a customer type)
If Customer Type NOT EQUAL TO Then
sSQLQry = sSQLQry & AND[Customer Type]= & CustomerType &
End If
myrs.Open sSQLQry, Conn
Paste data into sheet2 after clearing any earlier data
Sheet2.Visible = True
Sheet2.Select
Range(dataSet).Select
Range(Selection, Selection.End(xlDown)).ClearContents
Sheet2.Range(A2).CopyFromRecordset myrs
Close Recordset
myrs.Close
Close Connection
Conn.Close
End Sub
Note: Instead of NOT EQUAL TO youll have to use the proper brackets as shown in the video.
Details are also available at https://www.exceltrainingvideos.com/using-ado-and-sql-with-vba/ Using ADO and SQL with VBA](https://i.ytimg.com/vi/xuszn1iaOQ4/mqdefault.jpg)






