Create message boxes using VBA in Microsoft Excel

In this article, we will learn how to create Msgbox in Microsoft Excel. Msgbox is used to inform the user about the programming.

Through message box, we can specify the type of message with message buttons. In VBA, we have a list of buttons which shows in the message box. Have a look on list:-

Type Of buttons Description
vbOKOnly Ok button only
vbOKCancel ok and Cancel buttons
vbAbortRetryIgnore Abort, Retry and Ignore buttons
vbYesNoCancel Yes, No and Cancel buttons
vbYesNo Yes and No Buttons
vbRetryCancel Retry and Cancel Buttons
vbCritical Critical Sign will appear
vbQuestion Question Sign will appear
vbExclamation Exclamation Sign will appear
vbInformation Information Sign will appear
vbDefaultButton1 1st Button would be default
vbDefaultButton2 2nd Button would be default
vbDefaultButton3 3rd Button would be default
vbDefaultButton4 4th Button would be default
vbApplicationModal User have to respond before continue the work
vbSystemModal All applications are suspended until the user responds
vbMsgBoxHelpButton To add the Help button
VbMsgBoxSetForeground Specifies the message box window as the foreground window
vbMsgBoxRight Here text is right aligned
vbMsgBoxRtlReading Specifies text should appear as right-to-left reading on Hebrew and Arabic systems

Now we will learn how to create Msgbox. Follow below given steps: –

  • Open Excel Sheet and Press the key Alt+F11 to open the VBE.
  • In the code window, type below mentioned code: –

 

Sub Msg()
MsgBox “OnlineExcelTip !”
‘ messagebox with text and OK-button
MsgBox “OnlineExcelTip !”, vbInformation
‘ messagebox with text, OK-button and an information-icon
MsgBox ” OnlineExcelTip !”, vbCritical
‘ messagebox with text, OK-button and a warning-icon
MsgBox ” OnlineExcelTip !”, vbInformation, “My Title”
‘ messagebox with text, OK-button, information-icon and a custom title text
Answer = MsgBox(“Did you learn anything ?”, vbYesNo)
‘ messagebox with YES- and NO-buttons,
‘ the result is an integer, the constants are named vbYes and vbNo.
Answer = MsgBox(“Do you want to continue your learning with us ?”, vbYesNo + vbQuestion)
‘ messagebox with YES- and NO-buttons and a question-icon
Answer = MsgBox(“Are you satisfied ?”, vbYesNo + vbQuestion, “My Title”)
‘ messagebox with YES- and NO-buttons,
‘ question-icon and a custom title text
Answer = MsgBox(“Do we need any improvement ?”, vbYesNo + 256 + vbQuestion, “My Title”)
‘ messagebox with YES- and NO-buttons, question-icon and a custom title text,
‘ the NO-button is default
Answer = MsgBox(“Do you want to continue ?”, vbOKCancel, “My Title”)
‘ messagebox with OK- and CANCEL-buttons, the result is an integer,
‘ the constants are named vbOK og vbCancel.
End Sub

  • To run the Macro, press the key F5
  • You will get the message box with the information sign

Message Boxes:-
1st Image

 
2nd Image

 

These are the ways to write code for message boxes.

 

Previous Chapter Next Chapter

Leave a Reply