VBA MODULE 11
MSGBOX()
The MsgBox function is a dialogue box used to display information and to get simple user input.
Syntax :
MsgBox ( Prompt [,buttons (along with icon) ] [,title ] )
MsgBox Argument Description
ARGUMENT |
WHAT IT DOES |
Prompt |
It displays the text in the msgbox. This is always required to run the msgbox function. |
Buttons(Icon) |
It specifies which button to use along with the icon. This argument is optional so when we leave it blank it takes the default as 0 for button |
Title |
It displays the title of the msgbox . This is also optional so when we leave it blank it takes default value as the application name |
Let’s take a simple example where we only give the prompt Argument and for rest argument VBA will take the default value as discussed above.
Sub givemsg() MsgBox "Hello Dear" End Sub |
Result :
Customizing Message box
You can choose the button to display, determine which icon should appear, and decide which button is the default (the default button is “clicked” if the user presses Enter).
Table lists some of the built-in term and their value which you can use in your code for the buttons argument. If you prefer, you can use the value rather than a term in your code.
TERM |
VALUE |
BUTTON/ICON |
WHAT IT DOES |
vbOkOnly |
0 |
For Button |
Displays Ok button |
vbOkCancel |
1 |
For Button |
Displays OK and Cancel button |
vbAbortRetryIgnore |
2 |
For Button |
Displays Abort, Retry and Ignore buttons |
vbYesNoCancel |
3 |
For Button |
Displays Yes, No, Cancel buttons |
vbYesNo |
4 |
For Button |
Displays Yes, No buttons |
vbRetryCancel |
5 |
For Button |
Displays Retry and Cancel buttons. |
vbCritical |
16 |
For Icon |
Displays Critical Message icon. |
vbQuestion |
32 |
For Icon |
Displays Warning Query icon |
vbExclamation |
48 |
For Icon |
Displays Warning Message icon. |
vbInformation |
64 |
For Icon |
Displays Information Message icon. |
Let’s discuss the customization with an example. Please write the below code in your VBA editor and see the different customized msgboxes.
Sub CustomizedMessageBox() MsgBox "Hello Welcome to Message Box Customization" MegBox "See The Buttons Yes And No Below", vbYesNo MsgBox "See Your Message Title Above", "My Title" MsgBox "See InformationIcon At The Left", VbInformation MsgBox "This is Your Full Customized Message Box", VbYesNo + VbInformation, "My Title"
End Sub |
Return Values
The MsgBox function can return one of the following values which can be used to identify the button the user has clicked in the message box.
TERM |
VALUE |
WHAT IT MEANS |
vbOk |
1 |
User clicked OK. |
vbCancel |
2 |
User clicked Cancel. |
vbAbort |
3 |
User clicked Abort. |
vbRetry |
4 |
User clicked Retry. |
vbIgnore |
5 |
User clicked Ignore. |
vbYes |
6 |
User clicked Yes. |
vbNo |
7 |
User clicked No. |
Example
Below example will give you the clear picture of return value. Statement starting with the single inverted comma(‘) is just a comment to understand what we are doing and these lines are ignored when VBA executes the code. Please write this code in your VBA editor and see the result.
Function Returnvalue_Demo() 'Message Box with title and yes no Butttons a = MsgBox("Are you Genius?", vbYesNo,"Tell the Truth") ' if you press yes button, return value of a will be 6 and if no then 7 msgbox ("The Value of a is " & a) End Function |