Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
547 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Displaying a message box is a useful way to give your users information or instructions. This page describes how to display simple messages to the user.
We'll start by looking at how to show a simple message like this one.
You don't need any files for this page.
You can click here to download a file containing the sample code.
In any workbook, insert a new module in the VBE and create a subroutine called SimpleMessage. You can display a message to the user with the MsgBox function. You can see the list of parameters for the MsgBox function in the image below:
The tooltip appears when you type MsgBox followed by a space.
Although the MsgBox function has five parameters, you only need to pass a value to the first one, Prompt, in order to make it work. The code shown in the image below will make a simple message appear on screen:
Enclose your message in a set of double-quotes. Hopefully you can think of something more inspirational than the example shown here.
Although the tooltip shows the MsgBox parameters contained in parentheses (round brackets), you shouldn't actually type these in when you simply want to display a message on screen.
Executing the code shown above results in a basic message box appearing to the user, as shown below:
The exact appearance of the message box depends on the theme you have applied to Windows.
When the message box appears, your code will pause. When you click OK (or click the X in the top right corner) on the message box, your code will resume.
You can refer to a property of an object in the prompt of a message box. Create a subroutine called ShowAProperty and add code which displays the Name property of the workbook in a message box:
This code reads the name of the workbook into the message box.
Running the code shown above produces a message box similar to this:
The exact result will depend on the name of the workbook in which your code is stored.
You can return the result of a function to a message box and concatenate this with other information to build a message. Create a new subroutine called ShowAFunction and add code to display the current date as shown below:
This example concatenates a string with the result of the Date function.
If you have a particularly long message to display, you may prefer to store it in a variable before passing it to the message box. The code shown below concatenates a message and stores it in a variable. The message box refers to the variable in order to display its contents:
Storing the message text in a variable makes it much easier to write the code to display the message box. If you aren't working in Microsoft Windows you may wish to replace Environ("UserName") with Application.UserName for this example.
To practise displaying simple message boxes:
Sub MoreMessages()
'show a simple message
MsgBox "The only thing I cannot resist is temptation."
End Sub
You should see something similar to this when you run the subroutine.
Sub MoreMessages()
'show a simple message
MsgBox "The only thing I cannot resist is temptation."
'show another message
MsgBox "Quotation is a serviceable substitute for wit."
End Sub
To practise displaying values on a message box:
Sub ShowingOtherValues()
'show the name of the active sheet
MsgBox ActiveSheet.Name
End Sub
Sub ShowingOtherValues()
'show the name of the active sheet
MsgBox "You're looking at " & ActiveSheet.Name
End Sub
Sub ShowingOtherValues()
'show the name of the active sheet
MsgBox "You're looking at " & ActiveSheet.Name
'display the time and date
MsgBox "It is " & Time & " on " & Date
End Sub
Sub ShowingOtherValues()
Dim MsgTxt As String
'show the name of the active sheet
MsgBox "You're looking at " & ActiveSheet.Name
'display the time and date
MsgBox "It is " & Time & " on " & Date
End Sub
Sub ShowingOtherValues()
Dim MsgTxt As String
'show the name of the active sheet
MsgTxt = "You're looking at " & ActiveSheet.Name
MsgBox MsgTxt
'display the time and date
MsgBox "It is " & Time & " on " & Date
End Sub
Sub ShowingOtherValues()
Dim MsgTxt As String
'show the name of the active sheet
MsgTxt = "You're looking at " & ActiveSheet.Name
MsgBox MsgTxt
'display the time and date
MsgTxt = "It is " & Time & " on " & Date
MsgBox MsgTxt
End Sub
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.