The InputBox function returns an empty string when Cancel is pressed. Note it will also return an empty string if OK is pressed and there is nothing in the textbox.
Buddy008,
If u are assining value of the Input Box to a numeric variable , and u press cancel or ok without entering anything in the text box , then only it raises an error . Otherwise I never really needed any code for Cancel button
I have the Inputbox as a search, and I have it protected so that if you accidentally don't type any text an click ok a msgbox pops up and it allows you to try again. However if someone clicks on the search accidentally cancel won't work to exit the sub. so basically right now you must enter some sort of text to get out of the inputbox. I tried cancelerror = true and then an error handler to exit the sub, but no luck... any other suggestions??
You need to use the Msgbox function, ask the user if they wish to try again or cancel and provide two buttons. Check the MsgBox function return and proceed accordingly. Something like:
Dim intMR As Integer 'Msgbox function return
Dim strInput As String
Retry:
strInput = InputBox$("Enter search phrase", , _
"Text Search", 180, 180)
If strInput = vbNullString Then
'Get confirmation to cancel
intMR = Msgbox("Do you want to cancel the search?", _
vbYesNo, "Confirm Cancel"
If intMR = vbNo Then
GoTo Retry
Else
Exit Sub
End If
End If
Razvi, the information you provided about the return value of the InputBox function was worth knowing. I am sure most of the people like me were not aware of this... How to detemine whether the user entered a blank value or pressed the Cancel button...?
Have a star for providing this great piece of information. You really deserve it. (and many more from other members as well!)
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.