User Input Msgbox with Yes No Cancel

Some time the VBA code has to get approval from user with Yes/No/Cancel optop. Here is the simple code snippet that get the Yes, No, Cancel options in a popup window.

Example scenario for this would be like if there is any file created from Vba macro, the code can get user choice whether the file has to be saved or not.

So, here is the simple code snippet to implement this:

Sub msgboxYesNoCancel()
    Dim vUserInput As VbMsgBoxResult
        
    'Get User Input
    vUserInput = MsgBox("Do you want to save the file?", vbYesNoCancel)
    
    'Process User Input Yes,No,Cancel
    Select Case vUserInput
        Case vbYes
            'Code if User Input is Yes
        Case vbNo
            'Code if User Input is No
        Case vbCancel
            'Code if User Input is Cancel
    End Select

End Sub

Once user click any of the options, the Vba code get the user response and processes it accordingly.

There are other responses that could be made possible with msgbox. They are

  1. vbAbort
  2. vbCancel
  3. vbIgnore
  4. vbNo
  5. vbOk
  6. vbRetry
  7. vbYes

With msgbox second parameter, we can include buttons in a msgbox for combination of these user option buttons or any one as per your requirement.

External Reference: Msgbox function reference from Microsoft website.