We will learn how to create custom message boxes in Microsoft Excel using VBA. We will explore many VBA message functions include VbYesNo, Vb multiline function as well as some easy advanced message box processes.
Figure 1 – Using the vba msgbox custom button
Getting Started with VBA
Before we begin writing VBA code, let’s look at some syntax requirements when using the MSGBOX function:
=MSGBOX(Text_String , [buttons] ,[title], [helpfile, context])
- Text_String – This is the message text where we may enter what we want to say to the user. It can take up to 1024 characters and we must enclose it with double quotes
- Buttons – We may use this optional feature if we wish to make our message interactive. There are over 20 available buttons parameters we can use here. If we don’t want to use the button argument, then we will use the default type settings: vbOkayOnly
- Title – This is also an optional feature. We may choose to assign a custom tile or omit a title so our message will only display the name of the application.
- Help File – We can use this feature to specify the help file for use with the message box. It is equally an optional feature
- Context – This numeric parameter specifies the number assigned to a particular help topic. It is only applied when we have a help file argument.
Creating a VBA Message
To illustrate, we will create a message box containing “Hello Earthlings!”
- We will go to the Developer Tab, navigate to the code group and select Visual Basic
Figure 2 – How to use the vba display message
- In the VB Editor, we will right click on Microsoft Excel Objects and hover around Insert
- Next, we will select Module from the drop-down menu
Figure 3 – Macro msgbox
- It will open up to a blank module
- Here, we will type Sub firstMessage() without quotes and click Enter. Excel will immediately add the End Sub below the line.
Figure 4 – Vba show message box
- This means all our codes for the msgbox function must be entered above the End Sub
- We will type our syntax MsgBox followed by “Hello Earthlings!”.
Figure 5 – Vba show message box
- We will run the macro by clicking the Run Sub button or press F5
Figure 6 – Vba msgbox options
- Note that our default answer is set to “OK”
Adding a Title VBA message
We will use our syntax as in the first section, but now, we will add You are Beautiful. In this illustration, Hello Earthlings will be the title argument while You are Beautiful will be our Text_String.
We will insert as displayed in the below:
Sub secondMessage ()
MagBox “Hello Earthlings”, ,”You are Beautiful!”
Figure 7 – VBA msgbox options
When we click on the Run Sub, we will see this:
Figure 8 – vbexclamation
How to Use the Vb yes no box
To add the button, we will pick vbYesNOCancel, instead of the vbOKOnly key. Now we can change our VBA Message into a question using the figures below:
Sub YesOrNo ()
MsgBox “Do you know how lovely you look?”, vbYesNoCancel, “Hello Earthlings?”
Figure 9 -vbyesno
We will get a message like this:
Figure 10 – vba message box yes no
How to Add Multiple Lines of Text to the VBA message
Sub YesOrNo ( )
MsgBox “Are you at least 18 years of age?”, vbYesNoCancel, “Age?”
Figure 11 – VBA message options
We will see a message like this:
Figure 12 – VBA message options
Use the Advanced Technique Using the “if” Statement
When we want to add a second message box, we can use the if statement. Since our first VBA message may likely be a question that has about three answers, this routine will indicate the next step.
If we click “Yes”, then the subroutine will open the following message
If we click No, then the subroutine will generate another message
Sorry, try again later
VBA for this message:
If MsgBox("If you are 18 or older, click 'Yes'" & vbNewLine & "if you are not, click 'No'", vbYesNo, "Age?") _
= vbYes Then
MsgBox "Please proceed.", , "Result"
MsgBox "Sorry, you are not old enough.", , "Result"
Figure 13 – VBA yes no box
We will have a message like this:
Figure 14 – VBA message box
If we click Yes, then the sub message will read out:
Figure 15 – vb yes no
If we click No, then we will have this message:
Figure 16 – vb yes no
A VBA message box is a pop-style box we can use to program the behavior of using VBA. It provides an efficient way to interact with our workbooks since we will always receive alerts for specific actions.