Input boxes - how can I cancel out of it without generating an error message?

mkw4949

Board Regular
Joined
Apr 28, 2006
Messages
113
I am writing a simple macro and I am using the "input" fuction to gather some information from the user. So when someone clicks on the button attached to my macro an "InPut" box pups up and asks for the target value. That part works great but the problem is that if the user decided to click on the "cancel" button or hits "escape" an error message is generated. How Do I get arround this so that they user can stop running the macro by clicking on the "escape" button or hitting the "escapte" key. Thanks in advance.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Mike

Post your code.
Code:
Dim Ans As Variant
 
     Ans = InputBox("Please enter a number")
     
     If Not IsNumeric(Ans) Then
          MsgBox "Hey, you crazy fool!!" & vbCrLf & "You've either not entered a number, pressed Cancel or hit the Esc key." & vbCrLf & "So this macro is over."
          End
          
    End If
 
Upvote 0
THank you! I have one question for ya though. I changed the variable type for the "ans" variable from Varient to Double and it didnt work. Do you know why that is? I dont really need to know since what you gave me works great but I was just wondering. Thanks again!
 
Upvote 0
Mike

I assume you got a 'Type Mismatch' error?

Well I think that's one of the VBA error message that isn't encrypted too much.

Basically if you hit Cancel or press Esc the inputbox returns an empty string "".

That's obviously incompatible with the Double data type.

If you really need a variable to be of a certain data type there are plenty of data conversion functions you could use. eg CDbl, Val etc
 
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,576
Members
448,972
Latest member
Shantanu2024

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top