Using the cancel button to end a macro

K

kassie

I read that the inputbox does not have a cancel button? However, when I use
an inputbox, it has a cancel button on it. My question is, how do I exit a
macro if cancel is pressed?
 
G

Graham Mayor

This is well covered in VBA help on InputBox. Clicking cancel (which an
input box does have) produces a zero length string "" eg

sInput = InputBox("Enter your input")
If sInput = "" Then
MsgBox "Cancelled"
Exit Sub
End If
MsgBox sInput


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

Jonathan West

kassie said:
I read that the inputbox does not have a cancel button? However, when I
use
an inputbox, it has a cancel button on it. My question is, how do I exit
a
macro if cancel is pressed?

If the cancel button is pressed, the InputBox function returns an empty
string. Test for tthat and act accordingly.
 

Ask a Question

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.

Ask a Question

Top