PDA

View Full Version : [SOLVED:] Restrict InputBox to Numbers



Divad
09-13-2007, 02:17 AM
I have written some code so that when a button on the word toolbar is pressed it asks you how many pages you want to insert and it works fine. But if you try to enter something other than a number then it comes up with a debug, which it should.

What i want to do is restrict the InputBox to only numbers and have been trying a few things but it doesn't work properly. Is there an easy way to do this? Also can i put a limit on the number that they can enter?



Dim counter
Dim NoOfPages As String
NoOfPages = InputBox(Prompt:="Enter the number of additional pages you need", _
Title:="No. of additional pages", Default:="Enter number here")
If NoOfPages <> "0" & "5" Then
MsgBox "You can only enter a number in this field"

Else

For counter = 1 To NoOfPages

Thanks in advance

Nelviticus
09-13-2007, 05:03 AM
How about:


If Not IsNumeric(NoOfPages) Then

Regards

Nelviticus
09-13-2007, 05:08 AM
Or this:

Dim counter As Integer
Dim Pages As Integer
Dim NoOfPages As String
NoOfPages = InputBox(Prompt:="Enter the number of additional pages you need", _
Title:="No. of additional pages", Default:="Enter number here")
If Not IsNumeric(NoOfPages) Then
MsgBox "You can only enter a number in this field"
Else
Pages = Val(NoOfPages)
If Pages < 1 Or Pages > 5 Then
MsgBox "Pages must be between 1 and 5!"
Else
For counter = 1 To Pages
' Do stuff
Next
End If
End If
Regards

Divad
09-13-2007, 05:13 AM
You the man :)

Changed the Dim NoOfPages as you have to String and added in an ElseIf and it works great now. Many Many Thanks.


Dim counter
Dim NoOfPages As String
NoOfPages = InputBox(Prompt:="Enter the number of additional pages you need", _
Title:="No. of additional pages", Default:="Enter number here")
If Not IsNumeric(NoOfPages) Then
MsgBox "You must enter a numerical value."
ElseIf NoOfPages > 30 Then
MsgBox "Only a maximum of 30 additional pages may be added."
Else
For counter = 1 To NoOfPages