Locked lesson.
About this lesson
Identifying which button the user clicked when presented with a MsgBox in order to use their response in our code.
Exercise files
Download this lesson’s related exercise files.
Collecting feedback from a VBA MsgBox.xlsm26.1 KB Collecting feedback from a VBA MsgBox.xlsm
30.2 KB
Quick reference
Collecting feedback from a VBA MsgBox
Continuation of user feedback and input
When to use
To continue running a macro based on which message button a user selects
Instructions
- Create a Variant variable to record which button the user selects
- Test the message button selection to check the reponse
Syntax of a capture/test
Dim vAnswer As String
vAnswer = MsgBox("Message text", _
vbQuestion + vbYesNo, "Message Box Title")
If vAnswer = vbYes Then
'do something
Else
'do something else
End If
Hints & tips
- When setting the variable to equal a Message Box, you must include parentheses around the message and button code to capture the user’s selection
Lesson notes are only available for subscribers.