Lesson#119: What is Message Box in VBA?

What is Message Box?

A message box is a special dialog box used to display a piece of information to the user. The user cannot type anything in the message box. There are usually two kinds of message boxes you will create: one that simply displays information and one that expects the user to make a decision.

A message box is created using the MsgBox function. Its syntax is:

MsgBox(Prompt[, Buttons] [, Title] [, Helpfile, Context]) As VbMsgBoxResult

The MsgBox() function takes five arguments and only the first one is required.

Here is an example how can you create a simple message box by writing a code in VBA editor.

Sub System()
MsgBox (“Your system is being scanned”)
End Sub

This will display a message like this.

 

 

 

 

 

The Prompt argument can be made of up to 1024 characters. To display the Prompt on multiple lines, you should use the constant vbCrLf

Here is an example:

Sub System()
MsgBox (“Your system is being scanned” & vbCrLf & “It will be reboot after scanning”)
End Sub

It shows a message box like this. 

 

 

 

 

 

Configuring buttons of Message Box

Buttons argument specifies what button(s) should display on the message box. There are different types of buttons available in VBA

Here is an example how to call message box with button.

Sub System()
ActiveCell = MsgBox(“Your system is being scanned.” & vbCrLf & “Would you like to reboot after scanning?”, vbYesNo)
End Sub

It shows a message box like this.

 

 

 

 

 

Configuring icons of Message Box

You can configure icons in Message Box to make it more appealing to user.

Here is an example of Message box with icon.

Sub System()
ActiveCell = MsgBox(“Your system is being scanned.” & vbCrLf & “Would you like to reboot after scanning?”, vbYesNo Or vbQuestion)
End Sub

It shows a message box like this.

 

 

 

 

 

Title of Message Box

Now an example of message box in which the title is configured.

Sub System()
ActiveCell = MsgBox(“Your system is being scanned.” & vbCrLf & “Would you like to reboot after scanning?”, vbYesNo Or vbQuestion, “System Message”)
End Sub

Here how the message box shows.

 

 

 

 

 

The returned value of Message Box

the MsgBox() function returns a value by default which is defined in VBA language when user press a button. The values are these which are returned by buttons.

  • helpfile − An optional parameter and a string expression that identifies the helpfile to provide context help for the dialogue box.
  • context − An optional parameter and a numeric expression which identifies the help context number assigned by the author to the appropriate help topic.

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright 2017-2018@Excelabcd