Tuesday, September 25, 2012

Excel VBA - MsgBox


The Excel VBA MsgBox (message box) is a dialog box you can use in Excel VBA to display information to the users of your program. Below you can find three easy examples on how to create a MsgBox in Excel VBA.

1. To show a MsgBox with the message "This is fun", place a command button on your worksheet and simply add the following code line:
MsgBox "This is fun"

Result when you click the command button on the sheet:

Example of a Message Box

2. Now we try to create a little more advanced MsgBox.
Enter a random number into cell A1.
Instead of "This is Fun", add the following code line:
MsgBox "Entered value is " & Range("A1").Value

Result when you click the command button on the sheet:

More advanced MsgBox

We used the & operator to concatenate (join) two strings. Although Range("A1").value is not a string, it works here.

3. To start a new line in a message box, you can use vbNewLine. Add the following code line:
MsgBox "Line 1" & vbNewLine & "Line 2"

Result when you click the command button on the sheet:

MsgBox New Line

No comments:

Post a Comment