选择Case InputBox不起作用

时间:2015-08-21 12:32:00

标签: vba excel-vba excel

我写了一个程序

Sub message()

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

End Sub

但是当用户点击“取消”时,它实际上不会取消,而是清除单元格A1。

我试过这样的事情:

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

但它不起作用。

1 个答案:

答案 0 :(得分:1)

这是解决方案。

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