InputBox - cancel button

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 
Greg, the InputBox function will return a blank string ("") if Cancel is clicked.

Sub testCancel()
stest = InputBox("Press Cancel to see what is returned")
If stest = "" Then
MsgBox "You pressed Cancel"
Else
MsgBox "You didn't press Cancel."
End If
End Sub
 
Back
Top