Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Force Integer in Inputbox

Alex,
If you really mean an Integer (a whole number between -32,768 to 32,767) use
this, otherwise change accordingly for Long, Double etc :

Dim Question3 As String, Title3 As String
Dim ChangeLevel As Variant

Const DftInt As Integer = 0
Const NumbersOnly As Long = 1

Question3 = "What is the Change Level?"
Title3 = "Change Level"

ChangeLevel = Application.InputBox(Question3, Title3, DftInt, , , , ,
NumbersOnly)
If ChangeLevel = False Then
Exit Sub
End If
See if the result can coerced into an Int
On Error Resume Next
Range("E5").Value = CInt(ChangeLevel)
If Err.Number 0 Then
MsgBox "Out of range"
Range("E5").Value = CVErr(xlErrNum)
End If
On Error GoTo 0

NickHK

"Alex" wrote in message
...
I have the following code that inputs a value into a cell that is typed in

an
input box. How can I force the number that is typed in the input box to

be
an integer? Thanks.

Sub Input_Fields()

Dim Question3, Title3, Default3, Box3
Dim ChangeLevel As Integer
Question3 = "What is the Change Level?"
Title3 = "Change Level"
Default3 = ""
ChangeLevel = InputBox(Question3, Title3, Default3)


Range("e5").Select
ActiveCell.FormulaR1C1 = ChangeLevel


End Sub





  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 493
Default Force Integer in Inputbox

I used the following code in my module. I'm actually not going to set the
range value until later in the code so I remarked it out. My problem is that
if user leaves the changelevel blank an error box appears saying that,"the
formula you typed contains an error". In this code I just need the user to
be forced to type an integer, cannot leave it blank and cannot type text. If
user types text or leaves the box blank I need msgbox("you must type a
number"). What am I missing? Thanks.

Dim Question3 As String, Title3 As String
Dim ChangeLevel As Variant

Const DftInt As Integer = 0
Const NumbersOnly As Long = 1

Question3 = "What is the Change Level?"
Title3 = "Change Level"
NeedChangeLevel:
ChangeLevel = Application.InputBox(Question3, Title3, DftInt, , , , ,
NumbersOnly)
If ChangeLevel = False Then
Exit Sub
End If
'See if the result can coerced into an Int
On Error Resume Next
'Range("E5").Value = CInt(ChangeLevel)
If Err.Number < 0 Then
MsgBox "Must be a number"
GoTo NeedChangeLevel
' Range("E5").Value = CVErr(xlErrNum)
End If
On Error GoTo 0

"NickHK" wrote:

Alex,
If you really mean an Integer (a whole number between -32,768 to 32,767) use
this, otherwise change accordingly for Long, Double etc :

Dim Question3 As String, Title3 As String
Dim ChangeLevel As Variant

Const DftInt As Integer = 0
Const NumbersOnly As Long = 1

Question3 = "What is the Change Level?"
Title3 = "Change Level"

ChangeLevel = Application.InputBox(Question3, Title3, DftInt, , , , ,
NumbersOnly)
If ChangeLevel = False Then
Exit Sub
End If
See if the result can coerced into an Int
On Error Resume Next
Range("E5").Value = CInt(ChangeLevel)
If Err.Number 0 Then
MsgBox "Out of range"
Range("E5").Value = CVErr(xlErrNum)
End If
On Error GoTo 0

NickHK

"Alex" wrote in message
...
I have the following code that inputs a value into a cell that is typed in

an
input box. How can I force the number that is typed in the input box to

be
an integer? Thanks.

Sub Input_Fields()

Dim Question3, Title3, Default3, Box3
Dim ChangeLevel As Integer
Question3 = "What is the Change Level?"
Title3 = "Change Level"
Default3 = ""
ChangeLevel = InputBox(Question3, Title3, Default3)


Range("e5").Select
ActiveCell.FormulaR1C1 = ChangeLevel


End Sub






  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Force Integer in Inputbox

Perhaps something like this using InputBox (rather than Application.InputBox)

Sub Input_Fields()

Dim Question3, Title3, Default3, Box3
Dim ChangeLevel As String
Question3 = "What is the Change Level? Enter an integer number."
Title3 = "Change Level"
Default3 = ""

Dim longNum As Long
Dim dif As Double

On Error GoTo reset

retry:
ChangeLevel = InputBox(Question3, Title3, Default3)

If StrPtr(ChangeLevel) = 0 Then ' Cancel
MsgBox "Cancel not allowed. Entry is mandatory."
GoTo retry
'or
'Exit Sub

Else

If ChangeLevel = "" Then ' Blank
MsgBox "Blank not allowed. Entry is mandatory."
GoTo retry
Else
longNum = CLng(ChangeLevel)
dif = ChangeLevel - longNum

If dif < 0 Then
MsgBox "Integer entry only."
GoTo retry
End If
End If

End If

Range("E5").Value = ChangeLevel

Exit Sub

reset:
MsgBox "You may have entered text or the number is too large."
Resume retry

End Sub

"Alex" wrote:

I used the following code in my module. I'm actually not going to set the
range value until later in the code so I remarked it out. My problem is that
if user leaves the changelevel blank an error box appears saying that,"the
formula you typed contains an error". In this code I just need the user to
be forced to type an integer, cannot leave it blank and cannot type text. If
user types text or leaves the box blank I need msgbox("you must type a
number"). What am I missing? Thanks.

Dim Question3 As String, Title3 As String
Dim ChangeLevel As Variant

Const DftInt As Integer = 0
Const NumbersOnly As Long = 1

Question3 = "What is the Change Level?"
Title3 = "Change Level"
NeedChangeLevel:
ChangeLevel = Application.InputBox(Question3, Title3, DftInt, , , , ,
NumbersOnly)
If ChangeLevel = False Then
Exit Sub
End If
'See if the result can coerced into an Int
On Error Resume Next
'Range("E5").Value = CInt(ChangeLevel)
If Err.Number < 0 Then
MsgBox "Must be a number"
GoTo NeedChangeLevel
' Range("E5").Value = CVErr(xlErrNum)
End If
On Error GoTo 0

"NickHK" wrote:

Alex,
If you really mean an Integer (a whole number between -32,768 to 32,767) use
this, otherwise change accordingly for Long, Double etc :

Dim Question3 As String, Title3 As String
Dim ChangeLevel As Variant

Const DftInt As Integer = 0
Const NumbersOnly As Long = 1

Question3 = "What is the Change Level?"
Title3 = "Change Level"

ChangeLevel = Application.InputBox(Question3, Title3, DftInt, , , , ,
NumbersOnly)
If ChangeLevel = False Then
Exit Sub
End If
See if the result can coerced into an Int
On Error Resume Next
Range("E5").Value = CInt(ChangeLevel)
If Err.Number 0 Then
MsgBox "Out of range"
Range("E5").Value = CVErr(xlErrNum)
End If
On Error GoTo 0

NickHK

"Alex" wrote in message
...
I have the following code that inputs a value into a cell that is typed in

an
input box. How can I force the number that is typed in the input box to

be
an integer? Thanks.

Sub Input_Fields()

Dim Question3, Title3, Default3, Box3
Dim ChangeLevel As Integer
Question3 = "What is the Change Level?"
Title3 = "Change Level"
Default3 = ""
ChangeLevel = InputBox(Question3, Title3, Default3)


Range("e5").Select
ActiveCell.FormulaR1C1 = ChangeLevel


End Sub






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I add an integer to an existing integer? Aaron Excel Worksheet Functions 3 December 17th 09 09:46 PM
integer integer format Excel Worksheet Functions 1 May 3rd 07 06:45 PM
Inputbox and Application.InputBox Maria[_7_] Excel Programming 1 September 20th 04 11:36 AM
Get next Integer value Stuart[_5_] Excel Programming 7 February 16th 04 04:48 AM
Not seeing integer Martin Wheeler Excel Programming 1 September 4th 03 03:29 AM


All times are GMT +1. The time now is 12:24 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"