Excel VBA: How to capture MsgBox response

Viewed 9048

I would like to perform the following tasks:

  • write the message "Are you awake?" and display a question mark

  • capture the response in the integer variable intR, put intR value in cell A2

  • If the response is Yes, then write in cell A1 "Hurray"

  • If the response is No, then write a message box with the text "ZZZZZZZZ"

  • If the response is Cancel, then exit the sub

    Sub EX3_1_6MsgBoxFunction()
    
    Dim intR As Integer
    Dim TxtRng As Range
    Dim stra As String
    Dim stra2 As String
    
    'Have the message box display the buttons Yes, No and Cancel
    
    
    intR = MsgBox("Are you awake ? ", vbQuestion + vbYesNoCancel)
    intR = Range("a2")
    
    If intR = vbYes Then
    Range("a1") = "Hurray"
    
    'that means yes
    
    ElseIf intR = vbNo Then
    stra2 = MsgBox("ZZZZZZZZZZ")
    
    Else
    
    Range("a2") = intR
    
    
    End If
    
    End Sub
    
3 Answers
Related