Force InputBox to enter positive integer

edtully

New Member
Joined
Aug 14, 2002
Messages
6
I want to have an input box open up and ask the user to enter the number of items. I want to force this to be a positive integer. The following code (sort of) works:

While Not (no_of_items > 0)
no_of_items = InputBox("How many items?")
Wend

Two problems with this code
1) If the user enters a decimal it will accept it even though I want it to only accept integers.

2)If the user enters a string (ex: "two") then it will crash due to a type mismatch error since it is trying to evalute the no_of_items>0 statement with a string. If the user enters a string I would like to repeat the question until an integer is entered.

Any suggestion? Thanks in advance.
 
You can use Abs() function to return only positive numbers
You can use Round() function to return only integers if you enter 0 as the number of decimal places [ie: Round(TextBox1.Value,0) ]

Code:
Private Sub TextBox1_AfterUpdate()

If IsNumeric(TextBox1.Value) Then
    TextBox1.Value = Abs(Round(TextBox1.Value,0))
Else
    MsgBox ("You need to enter a whole number greater than zero. Please try again")
    TextBox1.Text = ""
End If

End Sub
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Fat Cat said:
You can use Abs() function to return only positive numbers
You can use Round() function to return only integers if you enter 0 as the number of decimal places [ie: Round(TextBox1.Value,0) ]

Code:
Private Sub TextBox1_AfterUpdate()

If IsNumeric(TextBox1.Value) Then
    TextBox1.Value = Abs(Round(TextBox1.Value,0))
Else
    MsgBox ("You need to enter a whole number greater than zero. Please try again")
    TextBox1.Text = ""
End If

End Sub

Thanx Fat Cat,

this kinda solve my prob, but i do not want the numbers to be rounded..rather i would like to reject a number wif decimals as well..anyway to do tat?
 
Upvote 0
We kind of got off the original topic of an InputBox but with a TextBox you could avoid data entry of any characters other than 0-9 by using the TextBox Change event:

Code:
Private Sub TextBox1_Change()
If Len(TextBox1.Text) > 0 Then
    Select Case Asc(Right(TextBox1.Text, 1))
        Case 0 To 46, 58 To 255
            TextBox1.Text = Left(TextBox1.Text, Len(TextBox1.Text) - 1)
    End Select
End If
End Sub
 
Upvote 0
For the InputBox, if you want to force an entry (this will not allow Cancel):

Code:
Sub test2()
Dim x
Do Until Val(x) = Int(Val(x)) And IsNumeric(x) And x <> ""
    x = InputBox("How many items?")
Loop
End Sub
 
Upvote 0
HOTPEPPER said:
For the InputBox, if you want to force an entry (this will not allow Cancel):

Code:
Sub test2()
Dim x
Do Until Val(x) = Int(Val(x)) And IsNumeric(x) And x <> ""
    x = InputBox("How many items?")
Loop
End Sub

thanx hotpepper..tink this (Val(x) = Int(Val(x)) And IsNumeric(x) And x <> "") was wad i needed most..i juz added (AND x > 0) into the conditions to get wad i wanted..i also decided to use it on a custom form instead of the inputbox..btw..y isnt there an InputBoxResult when there is MsgBoxResult for us to control the vbYEs and vbNo or in the case of InputBox vbCancel?? thanx a lot!!
 
Upvote 0
Cancel on an InputBox will just result in an empty string, so that's all you would have to check for.
 
Upvote 0
HOTPEPPER said:
Cancel on an InputBox will just result in an empty string, so that's all you would have to check for.

thanx for ur advice..managed to solve my probs as of now :LOL:

thanx for all the suggestions from everyone!!
 
Upvote 0

Forum statistics

Threads
1,215,580
Messages
6,125,654
Members
449,245
Latest member
PatrickL

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