Step 3 – Add exclamation logo Public Sub test3() SInput = MsgBox("Are you sure to delete the data?" & vbNewLine & "Data will be deleted permanently", vbOKCancel) Step 2 – Add OK / Cancel Button Public Sub test2() SInput = MsgBox("Are you sure to delete the data?" & vbNewLine & "Data will be deleted permanently") System modal all applications are suspended until the user responds to the message boxĮxample of Excel VBA MsgBox Step 1 – MsgBox with two lines of message Public Sub test1() Specifies the message box window as the foreground windowĭisplay OK button only (this is the default). Specifies text should appear as right-to-left reading on Hebrew and Arabic systems The user must respond to the message box before continuing work in the current application. If context is provided, helpfile must also be provided.īuttons Argument ( blue is about Modality)ĭisplay Abort, Retry, and Ignore buttons. Numeric expression that is the Help context number assigned to the appropriate Help topic by the Help author. If helpfile is provided, context must also be provided. String expression that identifies the Help file to use to provide context-sensitive Help for the dialog box. If you omit Title, the application name (Microsoft Excel) is placed in the title bar. String expression displayed in the title bar of the dialog box. The return values are as follows Return Value If you omit Buttons, the default value is zero (The user must respond to the message box before continuing work in the current application.)If you choose a Button, you need to define a variable to return a value, otherwise you will receive the following error message. You can add more than one Buttons using + sign, such as vbOKCancel + vbExclamation Numeric expression that is the sum of values specifying the number and type of buttons to display, the icon style to use, the identity of the default button, and the modality of the message box. If Prompt consists of more than one line, you can separate the lines usingģ) carriage return + line feed – Chr(13)& Chr(10) or vbCrLf ORĤ) vbNewLine, suitable for different platforms The maximum length of Prompt is approximately 1024 characters, depending on the width of the characters used. String expression displayed as the message in the dialog box.
In that case, you can insert a Msgbox code within a Procedure to check the value of a variable at specific line of code.Ģ) To warn user or confirm an action, such as asking users if they confirm to delete data Syntax of Excel VBA MsgBox MsgBox( prompt ) Prompt There are several reasons that you may need Excel VBA Msgbox:ġ) When you run a Sub or a Function, you will see the final result only and the result may not be what you expect while there is no error message. Note that there is another prompt box called InputBox which receives text input from user but it is not in the scope of this article.
Then, you need to enter the below given VBA code in the code window.This Excel tutorial explains how to use Excel VBA Msgbox with carriage return such as vbCr, vbNewLine, and handling of return value such as vbOK, vbCancel, vbRetryĮxcel VBA Inputbox and Application.Inputbox What is Excel VBA MsgBox?Įxcel VBA MsgBox is a prompt box that is used to prompt user for important information, usually provide user with OK and Cancel buttons.Then, on the Insert Tab, select the Module option from the menu.First, you need to open the Visual Basic Editor by clicking the shortcut ALT + F11 Keys.
Show Multiple Lines of String using MsgBox: A Numeric expression that identifies the Help context number assigned by the Help author to the appropriate Help topic. Context – This is also an optional one.
A String expression that identifies the Help file to use for providing context-sensitive help for the dialog box. If you omit this option, the application name is placed in the title bar. The String expression displayed in the title bar of the dialog box. The values specifying the number and type of buttons to display. The maximum length of the prompt is approximately 1024 characters.