Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Cancel with Input Box

Cancel with Input Box

Cancel with Input Box

Does anyone know how to handle Cancel with an Input Box, and distinguish it between an empty string when hitting Enter?  I've seen code that looks like:

Sub TestInput()
Dim ans As String
ans = InputBox("Response:")
If StrPtr(ans) = 0 Then
  MsgBox "You hit cancel"
  Exit Sub
ElseIf ans = "" Then
 MsgBox "You hit OK with no entry"
 Exit Sub
  MsgBox "Your answer is " & ans
End If

but when I tried this, and strptr(ans) seems to be a random number?  Any ideas what's missing?  Using Excel 2002 btw.

RE: Cancel with Input Box

The code you posted worked fine for me.  Needed to add an End Sub at the end, otherwise didn't have to do a thing.

I've got Excel 2000.

RE: Cancel with Input Box

I have Excel 2003 and it worked fine. Like Mint said, had to add the end sub line.

RE: Cancel with Input Box

The end sub is in my code (simply forgot to copy it to here), but it still doesn't explain why there is a random number generated by the strptr() function.

RE: Cancel with Input Box

I have never seen nor used the StrPtr() function, but it looks and smells to me like it is a pointer. Thus it may be returning an address that points to the data. Depending on what memory is available at the time, then it may return what appears to be a 'random' address. If cancel is selected, the pointer never gets assigned, and thus the call returns zero.

Just a hunch...

Wheels within wheels / In a spiral array
A pattern so grand / And complex
Time after time / We lose sight of the way
Our causes can't see / Their effects.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close