0

I wrote a procedure

Sub message()

    Dim answer As Variant
    answer = InputBox("Write something")
    Range("A1").Select
    ActiveCell.value = answer
    MsgBox "You wrote: " & answer

End Sub

but when a user clicks "Cancel" it actually doesn't cancel, but clear the cell A1.

I tried something like this:

Sub message()

    Dim answer As Variant
    answer =  Select Case InputBox("Write something")
    Case vbOK
        Range("A1").Select
        ActiveCell.value = answer
        MsgBox "You wrote: " & answer

End Sub

but it didn't work.

Community
  • 1
  • 1
maro
  • 473
  • 4
  • 11
  • 30
  • 1
    How can InputBox magically stop the rest of your code from executing if you press Cancel? If you press Cancel, [`vbNullString` is returned](http://stackoverflow.com/a/20909528/11683). – GSerg Aug 21 '15 at 12:37
  • possible duplicate of [VBA Password Input with Cancel Function](http://stackoverflow.com/questions/20909417/vba-password-input-with-cancel-function) – GSerg Aug 21 '15 at 12:55

1 Answers1

1

Here's the solution.

Sub message()

    Dim answer As Variant
    answer = InputBox("Write something")

    If StrPtr(answer) = 0 Then ''if Cancel pressed
        Exit Sub
    Else ''if OK pressed
        Range("A1").Value = answer
        MsgBox "You wrote: " & answer
    End If

End Sub
maro
  • 473
  • 4
  • 11
  • 30