Consulting

Results 1 to 4 of 4

Thread: Restrict InputBox to Numbers

  1. #1

    Restrict InputBox to Numbers

    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

  2. #2
    How about:

    If Not IsNumeric(NoOfPages) Then
    Regards
    Last edited by Aussiebear; 03-30-2023 at 02:21 PM. Reason: Adjusted the code tags

  3. #3
    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
    Last edited by Aussiebear; 03-30-2023 at 02:22 PM. Reason: Adjusted code tags

  4. #4
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •