Hi, I'm trying to create an input box that will exit the sub if the user clicks on "cancel". Here is my current code, but it ends the sub when the user does not key in anything and clicks 'OK'.
Dim strInput As StringstrInput = InputBox("Key in Item code")If strInput = vbNullString Then MsgBox ("Cancelled by User") Exit SubEnd If
How do I improve it such that clicking "cancel" will exit the sub, yet clicking "ok" when the user had not keyed in anything will prompt a different message ("Please key in Item code")?
Thanks in advance!
VBA
Last Comment
Pearlyn Tan
8/22/2022 - Mon
Rgonzo1971
Hi,
pls try
Dim strInput As StringstrInput = Application.InputBox("Key in Item code")If strInput = "False" Then MsgBox ("Cancelled by User") Exit SubEnd If
Dim strInput As StringstrInput = Application.InputBox("Key in Item code")If strInput = "False" Then MsgBox ("Cancelled by User") Exit SubEnd IfDo strInput = Application.InputBox("Please Key in Item code") If strInput = "False" Then MsgBox ("Cancelled by User") Exit Sub End IfLoop While strInput = ""
ok, but the code will error if user keyed in "False", it makes an expression that it was cancelled by user while user actually entered "False".
Pearlyn Tan
ASKER
Hi Rgonzo1971 and Ryan, thanks so much for both your codes, all worked perfectly. Personally preferred one with a message box prompt if user clicks ok with no input, but nonetheless I appreciate both. Thank you!
pls try
Open in new window
Regards