VBA - Message Box
The MsgBox function displays a message box and waits for the user to click a button and then an action is performed based on the button clicked by the user.
Syntax:-
MsgBox(prompt[,buttons][,title][,helpfile,context])
Parameter Description
- Prompt −A Required Parameter. A String that is displayed as a message in the dialog box. The maximum length of prompt is approximately 1024 characters. If the message extends to more than a line, then the lines can be separated using a carriage return character (Chr(13)) or a linefeed character (Chr(10)) between each line.
- Buttons −An Optional Parameter. A Numeric expression that specifies the type of buttons to display, the icon style to use, the identity of the default button, and the modality of the message box. If left blank, the default value for buttons is 0.
- Title − An Optional Parameter. A String expression displayed in the title bar of the dialog box. If the title is left blank, the application name is placed in the title bar.
- Helpfile − An Optional Parameter. A String expression that identifies the Help file to use for providing context-sensitive help for the dialog box.
- Context − An Optional Parameter. A Numeric expression that identifies the Help context number assigned by the Help author to the appropriate Help topic. If context is provided, helpfile must also be provided.
The Buttons parameter can take any of the following values −
- 0 vbOKOnly - Displays OK button only.
- 1 vbOKCancel - Displays OK and Cancel buttons.
- 2 vbAbortRetryIgnore - Displays Abort, Retry, and Ignore buttons.
- 3 vbYesNoCancel - Displays Yes, No, and Cancel buttons.
- 4 vbYesNo - Displays Yes and No buttons.
- 5 vbRetryCancel - Displays Retry and Cancel buttons.
- 16 vbCritical - Displays Critical Message icon.
- 32 vbQuestion - Displays Warning Query icon.
- 48 vbExclamation - Displays Warning Message icon.
- 64 vbInformation - Displays Information Message icon.
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.
- 1 - vbOK - OK was clicked
- 2 - vbCancel - Cancel was clicked
- 3 - vbAbort - Abort was clicked
- 4 - vbRetry - Retry was clicked
- 5 - vbIgnore - Ignore was clicked
- 6 - vbYes - Yes was clicked
- 7 - vbNo - No was clicked
Message Box with just prompt message
Function MessageBox_Demo()
MsgBox("Welcome")
End Function
Message Box with title, yes no and cancel Butttons
Function MessageBox_Demo()
int a = MsgBox("Do you like blue color?",3,"Choose options")
msgbox ("The Value of a is " & a)
End Function
Function MessageBox_Demo()
MsgBox("Welcome")
End Function
Message Box with title, yes no and cancel Butttons
Function MessageBox_Demo()
int a = MsgBox("Do you like blue color?",3,"Choose options")
msgbox ("The Value of a is " & a)
End Function
VBA - InputBox
The InputBox function prompts the users to enter values. After entering the values, if the user clicks the OK button or presses ENTER on the keyboard, the InputBox function will return the text in the text box. If the user clicks the Cancel button, the function will return an empty string ("").
Syntax:-
InputBox(prompt[,title][,default][,xpos][,ypos][,helpfile,context])
Parameter Description
- Prompt −A required parameter. A String that is displayed as a message in the dialog box. The maximum length of prompt is approximately 1024 characters. If the message extends to more than a line, then the lines can be separated using a carriage return character (Chr(13)) or a linefeed character (Chr(10)) between each line.
- Title − An optional parameter. A String expression displayed in the title bar of the dialog box. If the title is left blank, the application name is placed in the title bar.
- Default − An optional parameter. A default text in the text box that the user would like to be displayed.
- XPos − An optional parameter. The position of X axis represents the prompt distance from the left side of the screen horizontally. If left blank, the input box is horizontally centered.
- YPos − An optional parameter. The position of Y axis represents the prompt distance from the left side of the screen vertically. If left blank, the input box is vertically centered.
- Helpfile − An optional parameter. A String expression that identifies the helpfile to be used to provide context-sensitive Help for the dialog box.
- context − An optional parameter. A Numeric expression that identifies the Help context number assigned by the Help author to the appropriate Help topic. If context is provided, helpfile must also be provided.
Example:-
Function findArea()
Dim Length As Double
Dim Width As Double
Length = InputBox("Enter Length ", "Enter a Number")
Width = InputBox("Enter Width", "Enter a Number")
findArea = Length * Width
End Function