If you use the Excel inputbox, you can trap the cancel.
For example:
Sub DemoInputbox()
Dim res As Variant
res = Application.InputBox("What is your name?")
If res = False Then
MsgBox "You cancelled!"
Exit Sub
ElseIf res = "" Then
MsgBox "You failed make an entry!"
Else
MsgBox "You entered " & res
End If
I"m looking for answer to same question except my input returns an integer answer so when I try to use this (or the other suggestions to this post) I get a type mismatch errro.
Any help is always appreciated
An inputbox (as opposed to a message box) returns value "" if Cancel is
pressed. So the code below should work with an inputbox
Sub InputTest()
Dim Answer As String
Answer = InputBox("Enter value")
If Answer = "" Then
MsgBox ("Cancel pressed")
Exit Sub
Else
MsgBox ("Value entered: " & Answer)
End If
End Sub
For a messagebox the vbCancel does work:
if msgbox("Hi there",vbOKCancel) = vbCancel then msgbox("Cancel pressed")
jeffP said:
I"m looking for answer to same question except my input returns an integer
Hi JeffP
In addition to Binzelli's answer:
also consider using Application.InputBox which restricts the type of input
expected:
MyNumber = Application.InputBox("Give a number please", "Number ?", , , , ,
, 1) '1 = Number see help for other types
If MyNumber <> False Then
MsgBox "The number is " & MyNumber
Else: MsgBox "Cancelled was pressed", vbInformation
End If
HTH
Cordially
Pascal
jeffP said:
I"m looking for answer to same question except my input returns an integer
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.