Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cancel with Input Box

Status
Not open for further replies.

pugap

Mechanical
Nov 18, 2003
45
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
Else
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.
 
Replies continue below

Recommended for you

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.
 
I have Excel 2003 and it worked fine. Like Mint said, had to add the end sub line.
 
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.
 
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.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor