maro
maro

Reputation: 503

Select Case InputBox doesn't work

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.

Upvotes: 0

Views: 1499

Answers (1)

maro
maro

Reputation: 503

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

Upvotes: 1

Related Questions