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
 

Some videos you may like

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Makrini

Well-known Member
Joined
May 22, 2007
Messages
1,035
Code:
Sub tester()
  Test = InputBox("Enter a Number")
  
  If IsNumeric(Test) Then
    MsgBox ("Correct Entry")
    Else: MsgBox ("user Cancelled")
  End If
End Sub
 
L

Legacy 68668

Guest

ADVERTISEMENT

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
 

9tanstaafl9

Well-known Member
Joined
Mar 23, 2008
Messages
535
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.
 

9tanstaafl9

Well-known Member
Joined
Mar 23, 2008
Messages
535

ADVERTISEMENT

Just want to apologize, I only today realized there was a thank/like option for the posts. I really do appreciate the help.
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,910
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
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,686
Office Version
  1. 2010
Platform
  1. Windows
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:

Watch MrExcel Video

Forum statistics

Threads
1,122,667
Messages
5,597,458
Members
414,145
Latest member
lonnie451

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
Top