Well good morning, so here is what msgbox dialog function in excel VBA does!!

Hmm..IF you are already familiar with the msgbox, you can probably answer the below Question?

What an msgbox does and when we use this in our program?

Here My Understanding of this …..

Msgbox is a function in excel VBA macro, which displays a dialog box with the predefined message on it (a message user want to display) and it also contains the buttons which user clicks after reading the message (a button clicked is the result of the user decision after reading the message) and your program converts the button press from the user to a integer value and progress based on it.


Woops,, it’s too much theory here; now let’s move to some practical stuff.

Here is the syntax of msgbox >>

Msgbox (text string [, buttons] [, title] [,helpfile , context])

Parts highlighted in green are mandatory, others are optional.

Here ‘Text_String’ is the message that you want the msgbox to display. The maximum length of ‘Text_String’ is 1024 characters.

‘buttons’ parameter specifies the type of buttons and icon that you want to be shown on the message box. It is an optional parameter. If you omit it then msgbox shows the default ‘vbOKOnly’ button.

‘title’ refers to the text displayed in the title bar of message box. This is an optional parameter.

‘helpfile’ is a string parameter that specifies the help file to be used for the dialog box. It is also an optional parameter but it becomes mandatory if ‘context’ parameter is to be used.

‘context’ is a numeric parameter that specifies the number assigned to the appropriate Help topic. It is an optional parameter but it becomes mandatory if ‘helpfile’ parameter is used.


Basic message box.


Sub displayText()

‘this is a simple msgbox to display some message to user,remember only OK button is displayed to user

MsgBox “Welcome mr. Jack”

End Sub


Sub whichbutton()

‘this msgbox appears showing the user with the buttons which they can use as a chioce

MsgBox “Do you want to continue?”, vbYesNo

‘this msgbox appears showing the user a OK button (this can be done if you left the Button option blank)

MsgBox “Press Ok to continue”, vbOKOnly

‘this msgbox

End Sub


Constant Value Description
vbOKOnly 0 Display OK button only.
vbOKCancel 1 Display OK and Cancel buttons.
vbAbortRetryIgnore 2 Display Abort, Retry, and Ignore buttons.
vbYesNoCancel 3 Display Yes, No, and Cancel buttons.
vbYesNo 4 Display Yes and No buttons.
vbRetryCancel 5 Display Retry and Cancel buttons.
vbCritical 16 Display Critical Message icon.
vbQuestion 32 Display Warning Query icon.
vbExclamation 48 Display Warning Message icon.
vbInformation 64 Display Information Message icon.
vbDefaultButton1 0 First button is default.
vbDefaultButton2 256 Second button is default.
vbDefaultButton3 512 Third button is default.
vbDefaultButton4 768 Fourth button is default.
vbApplicationModal 0 Application modal; the user must respond to the message box before continuing work in the current application.
vbSystemModal 4096 System modal; all applications are suspended until the user responds to the message box.
vbMsgBoxHelpButton 16384 Adds Help button to the message box
VbMsgBoxSetForeground 65536 Specifies the message box window as the foreground window
vbMsgBoxRight 524288 Text is right aligned
vbMsgBoxRtlReading 1048576 Specifies text should appear as right-to-left reading on Hebrew and Arabic systems