Create message box with yes no option in vba excel

vba yes no message box

Objective

To create a message box with Yes and No option.

Approach

Here we have created a message box which has options as “Yes” and “No”, we can use the user response further in our code. Here in the below shown code, if the user selects “Yes” then, we shall store the string “Yes” in cell A1, while if the user selects “No” in the input box, we shall store “No” in cell A1.

We can use the value which we have stored in cell A1 for any formula, or we can store the user response in a variable and then we can use it further in the code. We can also call another subroutine based on user input. In the code below, I have commented the part where we are invoking the subroutine TestProcedure.We can customize the code based upon our requirement.

In the image below, we can see that when we run the code shown here, a message box will be displayed.

 

vba yes no message box

 

Code

Sub YesNoInputBox()

Dim MyUserInput As String

'Display the message box with yes and no option
MyUserInput = MsgBox("Do you want to select file", vbYesNo, "Select File")

If MyUserInput = vbYes Then
Range("A1").Value = "Yes"
'We can also call another subprocedure if needed
'Call TestProcedure

Else
Range("A1").Value = "No"
End If
End Sub


 

Post you may like

Import or copy text file data to excel sheet in VBA Excel

Reference

https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/msgbox-function