One often wishes to show a message box to a user to inform them that a particular operation has completed. Sometimes there are further operations to complete, but these will not start until the user has closed the message box.
This technique creates a message box (actually a UserForm) and shows it for a specified time. After that specified time, the message box will close and allow the VBA to continue.
First create a User Form named FlashForm. The form should have 2 labels on the form named MainLabel and TimedLabel and a command button named OKButton.
The UserForm_Activate code (listed below) will set the labels etc to the right size and position.
Copy the code below to the user form code sheet.
[color blue]
Private Sub OKButton_Click()
ForceOK
End Sub
Private Sub UserForm_Activate()
With Me
.Height = 190
.Width = 230
.MainLabel.Height = 40
.MainLabel.Width = 210
.MainLabel.Left = 10
.MainLabel.Top = 12
.TimeLabel.Height = 24
.TimeLabel.Width = 210
.TimeLabel.Left = 10
.TimeLabel.Top = 66
.OKButton.Height = 36
.OKButton.Width = 210
.OKButton.Left = 10
.OKButton.Top = 102
End With
TimeSet = Now + TimeValue("00:00:" & Format(T, "00"))
Application.OnTime TimeSet, "ForceOK"
End Sub
[/color]
Now copy the code below to a standard module.
[color blue]
Public TimeSet As Variant
Public OKClicked As Boolean
Public T As Integer
Sub ShowQuickMessage()
ShowFlashForm "Sheet 1 has now printed", 10
End Sub
Sub ShowFlashForm(Title As String, Time As Integer)
Load FlashForm
T = Time
With FlashForm
.TimeLabel.Caption = "This display will disappear in " & T & " seconds."
.Caption = "INFORMATION MESSAGE"
.MainLabel.Caption = Title
.Show
End With
Unload FlashForm
OKClicked = False
End Sub
Sub ForceOK()
On Error Resume Next
With FlashForm
.Hide
End With
Application.OnTime TimeSet, "ForceOK", , False
OKClicked = True
Unload FlashForm
End Sub
[/color]
The principle behind this is that you use the code
[color red]ShowFlashForm "Sheet 1 has now printed", 10 [/color]
to show the Flashform with the MainLabel showing the text for 10 seconds.
The FlashForm is activated and as part of that activation it starts the [color blue]OnTime[/color] function. This means that at the [color blue]TimeSet[/color] , which is T seconds after the form was activated, then it will call the macro [color blue]ForceOK[/color] which is also called by clicking the OK button.
This macro closes the FlashForm and stops the OnTime function.
I use this technique in a macro as follows:
Sub MyUse()
Backup C: [color green]- a routine that takes a few minutes [/color]
ShowFlashForm "C backed up", 5
Backup D: [color green]- a second routine that takes a few minutes [/color]
ShowFlashForm "Both drives backed up", 10
End Sub
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.