+ Reply to Thread
Results 1 to 2 of 2

how to limit user to input a number within specific range?

  1. #1
    Registered User
    Join Date
    09-24-2009
    Location
    singapore
    MS-Off Ver
    Excel 2007
    Posts
    3

    how to limit user to input a number within specific range?

    I just started learning VBA and I am trying to ask user to input
    a number between 1 and 1000, and I wrote the following code:

    ----------------------------------------------------------------------------------------
    Sub Simu()
    Dim Iter As Variant
    Dim Msg As String
    Dim Ans As Integer

    EnterAgain:
    On Error GoTo BadEntry
    Iter = InputBox("How many iterations do you want? Please enter a number between 1 and 1000", Simulation)
    If Iter = "" Then Exit Sub

    If (Iter < 1 Or Iter > 1000) Then GoTo BadEntry
    Call Reset
    For i = 1 To Iter
    Application.Calculate
    Next i
    Exit Sub

    BadEntry:
    Msg = "Your have entered an invalid number, you want to enter again?"
    Ans = MsgBox(Msg, vbYesNo)
    If Ans = vbYes Then Resume EnterAgain

    End Sub
    -------------------------------------------------------------------------------------------------------------

    Everything works fine except that the yes/no msgbox will always pop up twice
    before the inputbox pops up. That means Excel will ask twice if the user want to enter
    a number again...Can anyone help?

  2. #2
    Forum Expert
    Join Date
    12-23-2006
    Location
    germany
    MS-Off Ver
    XL2003 / 2007 / 2010
    Posts
    6,326

    Re: how to limit user to input a number within specific range?

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1