Test InputBox for Cancel?

F

Fred Holmes

How Does one test whether the user has pressed Cancel on an input box?

myString = InputBox(. . . .

If myString = vbCancel Then Exit Sub

The above fails. Pressing Cancel does not trigger the if statement.

MS Word 2003
 
J

Jay Freedman

Fred said:
How Does one test whether the user has pressed Cancel on an input box?

myString = InputBox(. . . .

If myString = vbCancel Then Exit Sub

The above fails. Pressing Cancel does not trigger the if statement.

MS Word 2003

Hi Fred,

Use
If myString = ""
or
If Len(myString) = 0

There's no way in an InputBox to distinguish between hitting the Cancel
button and simply leaving the box blank and hitting OK. Usually there's no
reason to need that distinction. If you do need it, use a userform instead
of an InputBox -- that also has the advantage that the user can fill in
multiple items and only click OK once.
 

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