Module 5 - Messages and User Inputs
Lesson 5.1 - Displaying Messages
Topic 5.1.3 - Displaying Icons

You can make a message box look more interesting by adding an icon to it.  This page describes how to display icons on a message box.

Icon

Icons are a good way to make a message look more intimidating.

Files Needed

You don't need any files for this section.

Completed Code

You can click here to download a workbook which contains the completed code.

To begin this part of the lesson, create a new workbook and in the VBE insert a new module.

Displaying Icons on a Message Box

To change the icon displayed on the message, confusingly you use the Buttons parameter.  You can see a list of options for this parameter by typing a comma after the prompt of a message box.  Create a new subroutine called ChangingTheIcon and add the code shown in the diagram below:

Choosing icon

You can choose which icon to use from the list of constants which appears.

If the list of constants disappears, you can press CTRL + SHIFT + J to redisplay it.

Choose the vbExclamation constant from the list:

Basic icon

Well, you can't say you weren't warned.

Running this code produces the message box shown below:

Warning message

I'm not quite clear on what we're being warned about.

 

The Available Icons

You can display one of four icons on a message box, as shown in the table below:

Constant Icon Sample code
vbCritical MsgBox "Something went wrong!", vbCritical
vbExclamation MsgBox "This might be a bad idea!", vbExclamation
vbInformation MsgBox "Just FYI", vbInformation
vbQuestion MsgBox "Are you sure?", vbQuestion

Using Named Parameters

You might find your code more readable if you use named parameters and continuation characters.  Create a new subroutine called NamingArguments and add code to it as shown below:

Named arguments

Using multiple lines and named arguments means more typing for you, but it makes your code more readable.

 

To practise adding an icon to a message box:

  1. Using any workbook, open the VBE and insert a new module.
  2. Create a subroutine called CustomisingMessages and add the code shown below (feel free to alter the prompt):

Sub DisplayAnIcon()

 

MsgBox "Learning never exhausts the mind"

 

End Sub

  1. Alter your code to display an icon on the message box:

Sub DisplayAnIcon()

 

MsgBox "Learning never exhausts the mind", vbInformation

 

End Sub

Remember that there are four different icons: vbCritical, vbExclamation, vbInformation and vbQuestion.  Use the one that you feel is most appropriate for your message.

  1. Test your code by clicking within the subroutine and pressing F5 to run it.
Result

I don't think that da Vinci had ever tried to learn VBA when he said this.

 
  1. Alter the code to use named arguments and continuation characters.

Sub DisplayAnIcon()

 

MsgBox _

Prompt:="Learning never exhausts the mind", _

Buttons:=vbInformation

 

End Sub

  1. Add a second message box with a different message and icon.

Sub DisplayAnIcon()

 

MsgBox _

Prompt:="Learning never exhausts the mind", _

Buttons:=vbInformation

 

MsgBox _

Prompt:="So why do I feel so tired?", _

Buttons:=vbQuestion

 

End Sub

  1. Run the subroutine to check that you see each message then save and close the workbook.
This page has 0 threads Add post