ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Whole Number Validation in Input Box (https://www.excelbanter.com/excel-programming/327227-whole-number-validation-input-box.html)

Dkt24

Whole Number Validation in Input Box
 
I am having problems with an input box. The user can only put in a whole
number greater than or equal to 3. If they put in text or a decimal, I want
it to display an error and reopen the dialogue box. I got the text part, but
not the decimal validation. Any suggestions? The example of the code thus
far is written below.

'The user is now asked to input the number of sides in the box.
'The only valid answers is a numeric whole number greater than or equal to 3

Dim c As Integer, sides As Variant
c = 3
sides = Application.InputBox("Please enter the number of sides of your box",
"Box Geometry", c)
If sides = False Then GoTo 100
If sides = "" Or Not IsNumeric(sides) Then GoTo 2 Else GoTo 3
2
Repsonse = MsgBox("Your input must be a whole number greater than 3. Try
again.", vbOKOnly, "Error")
GoTo 1
3
If sides < 3 Then GoTo 2 Else GoTo 4
4
100

Bernie Deitrick

Whole Number Validation in Input Box
 
Dkt24,

Try it like below. Wasn;t sure whether 3 was a vlaid entry or not, since
your message and your math conflicted.

If three is Ok, then change sides<=3 back to sides <3 and change
the "greater then 3" part of your message to "3 or greater"

HTH,
Bernie
MS Excel MVP


Sub TryNow()
Dim c As Integer, sides As Variant
c = 3
GetEntry:
sides = Application.InputBox( _
"Please enter the number of sides of your box", _
"Box Geometry", c)
If sides = False Then Exit Sub
If sides = "" Or Not IsNumeric(sides) Or _
sides <= 3 Or (CInt(sides) < sides) Then
MsgBox "Your input must be a whole number greater than 3." & _
"Try again.", vbOKOnly, "Error"
GoTo GetEntry
End If
MsgBox "The valid value entered was " & sides & "."
End Sub

"Dkt24" wrote in message
...
I am having problems with an input box. The user can only put in a whole
number greater than or equal to 3. If they put in text or a decimal, I

want
it to display an error and reopen the dialogue box. I got the text part,

but
not the decimal validation. Any suggestions? The example of the code

thus
far is written below.

'The user is now asked to input the number of sides in the box.
'The only valid answers is a numeric whole number greater than or equal to

3

Dim c As Integer, sides As Variant
c = 3
sides = Application.InputBox("Please enter the number of sides of your

box",
"Box Geometry", c)
If sides = False Then GoTo 100
If sides = "" Or Not IsNumeric(sides) Then GoTo 2 Else GoTo 3
2
Repsonse = MsgBox("Your input must be a whole number greater than 3. Try
again.", vbOKOnly, "Error")
GoTo 1
3
If sides < 3 Then GoTo 2 Else GoTo 4
4
100




Dave Peterson[_5_]

Whole Number Validation in Input Box
 
You could just truncate the value they give you:

sides = int(sides)

or you could check
if sides = int(sides) then
'no decimals
else
'decimals
end if


Dkt24 wrote:

I am having problems with an input box. The user can only put in a whole
number greater than or equal to 3. If they put in text or a decimal, I want
it to display an error and reopen the dialogue box. I got the text part, but
not the decimal validation. Any suggestions? The example of the code thus
far is written below.

'The user is now asked to input the number of sides in the box.
'The only valid answers is a numeric whole number greater than or equal to 3

Dim c As Integer, sides As Variant
c = 3
sides = Application.InputBox("Please enter the number of sides of your box",
"Box Geometry", c)
If sides = False Then GoTo 100
If sides = "" Or Not IsNumeric(sides) Then GoTo 2 Else GoTo 3
2
Repsonse = MsgBox("Your input must be a whole number greater than 3. Try
again.", vbOKOnly, "Error")
GoTo 1
3
If sides < 3 Then GoTo 2 Else GoTo 4
4
100


--

Dave Peterson

Dkt24

Whole Number Validation in Input Box
 
It works! Thanks, Bernie.

"Bernie Deitrick" wrote:

Dkt24,

Try it like below. Wasn;t sure whether 3 was a vlaid entry or not, since
your message and your math conflicted.

If three is Ok, then change sides<=3 back to sides <3 and change
the "greater then 3" part of your message to "3 or greater"

HTH,
Bernie
MS Excel MVP


Sub TryNow()
Dim c As Integer, sides As Variant
c = 3
GetEntry:
sides = Application.InputBox( _
"Please enter the number of sides of your box", _
"Box Geometry", c)
If sides = False Then Exit Sub
If sides = "" Or Not IsNumeric(sides) Or _
sides <= 3 Or (CInt(sides) < sides) Then
MsgBox "Your input must be a whole number greater than 3." & _
"Try again.", vbOKOnly, "Error"
GoTo GetEntry
End If
MsgBox "The valid value entered was " & sides & "."
End Sub

"Dkt24" wrote in message
...
I am having problems with an input box. The user can only put in a whole
number greater than or equal to 3. If they put in text or a decimal, I

want
it to display an error and reopen the dialogue box. I got the text part,

but
not the decimal validation. Any suggestions? The example of the code

thus
far is written below.

'The user is now asked to input the number of sides in the box.
'The only valid answers is a numeric whole number greater than or equal to

3

Dim c As Integer, sides As Variant
c = 3
sides = Application.InputBox("Please enter the number of sides of your

box",
"Box Geometry", c)
If sides = False Then GoTo 100
If sides = "" Or Not IsNumeric(sides) Then GoTo 2 Else GoTo 3
2
Repsonse = MsgBox("Your input must be a whole number greater than 3. Try
again.", vbOKOnly, "Error")
GoTo 1
3
If sides < 3 Then GoTo 2 Else GoTo 4
4
100






All times are GMT +1. The time now is 08:47 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com