View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default InputBox - cancel button

Cancel returns an empty string.

Dim ans as String
ans = InputBox("entry please")

if ans = "" then
Msgbox "Response not acceptable, exiting"
exit sub
End if

The user could hit OK without making an entry and this would be interpreted
as a cancel. If you want to differentiate, then

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

End Sub

--
Regards,
Tom Ogilvy

"Greg Billinge" wrote in message
...
Hi,

how do you detect if the user presses 'Cancel' on an InputBox? - i want to
exit sub if this button is pressed

Many thanks in advance

Greg