How exit sub on InputBox Cancel if Zero is valid?

9tanstaafl9

Well-known Member
Joined
Mar 23, 2008
Messages
535
Sorry if this is dumb, but how do I differentiate between a user entering a zero and a user clicking on Cancel on my input box? Zero would be a valid response and my code should continue. Valid responses are 0 through 12.

I've been looking for two hours and can't figure this out.

Thanks.
Jennifer
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Code:
Sub tester()
  Test = InputBox("Enter a Number")
  
  If IsNumeric(Test) Then
    MsgBox ("Correct Entry")
    Else: MsgBox ("user Cancelled")
  End If
End Sub
 
Upvote 0
Use InputBox method, instead of InputBox function
Something like
Code:
Dim myNum
myNum = Application.InputBox("Enter number 0 - 12", type:=1)
If myNum = False Then Exit Sub
If (myNum < 0) + (myNum > 11) Then
    MsgBox "Invalid number"
End If
 
Upvote 0
Sorry this reply is a bit late. I hadn't checked the thread again after the reply before yours, and I didn't see your response. The bit of code you provided helped a lot with a newer version of the report and I just wanted to say thanks.
 
Upvote 0
Just want to apologize, I only today realized there was a thank/like option for the posts. I really do appreciate the help.
 
Upvote 0
The InputBox (as opposed to Application.InputBox) always returns a string.
You can use StrPtr to detemine if the use has pressed cancel or entered a blank value
Code:
Dim uiResponse As String

uiResponse = InputBox("Input a number")

If StrPtr(uiResponse) = 0 Then
    MsgBox "user canceled"
Else
    If uiResponse = vbNullString Then
        MsgBox "user entered blank"
    Else
        MsgBox "user entered " & uiResponse
    End If
End If
 
Upvote 0
The InputBox (as opposed to Application.InputBox) always returns a string.
You can use StrPtr to detemine if the use has pressed cancel or entered a blank value
Code:
Dim uiResponse As String

uiResponse = InputBox("Input a number")

If StrPtr(uiResponse) = 0 Then
    MsgBox "user canceled"
Else
    If uiResponse = vbNullString Then
        MsgBox "user entered blank"
    Else
        MsgBox "user entered " & uiResponse
    End If
End If
This is basically the same code that Mike posted, but it is structured slightly differently...
Code:
Dim Answer As String

Answer = InputBox("Tell me something")

If StrPtr(Answer) = 0 Then
  MsgBox "The user clicked Cancel, so we will exit the subroutine now."
  Exit Sub
ElseIf Len(Answer) = 0 Then
  MsgBox "The user clicked OK without entering anything in the InputBox!"
Else
  MsgBox "The user entered the following..." & vbLf & vbLf & Answer
End If
 
Last edited:
Upvote 0

Forum statistics

Threads
1,217,763
Messages
6,138,457
Members
450,139
Latest member
Sushmita Ramesh Kulkarni

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top