Lesson#119: What is a Message Box in VBA?

Lesson#119: What is a Message Box in VBA?

What is a 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 of how can you create a simple message box by writing code in the 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

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

Here is an example of how to call a message box with a 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 users.

See also  Lesson#190: Continuously autosave the workbook with VBA

Message Box in VBA

Here is an example of a Message box with an 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.Message Box in VBA

Title of Message Box

Now is an example of a 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 is how the message box shows.Message Box in VBA

The returned value of Message Box

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

Message Box in VBA

  • 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 that identifies the help context number assigned by the author to the appropriate help topic.

Hi! I am Puspendu. I am the founder and author of Excelabcd. I am little creative person, blogger and Excel-maniac guy. I hope you enjoy my blog.

Leave a Reply

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

*