+ Reply to Thread
Results 1 to 3 of 3

how to limit user input between 1 to 1000

Hybrid View

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

    how to limit user input between 1 to 1000

    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:
    'if any run time error happens, excute BadEntry
    On Error GoTo BadEntry
    'Ask user to enter a number between 1 to 1000"
    Iter = InputBox("How many iterations do you want? Please enter a number between 1 and 1000", Simulation)
    'if nothing entered, stop the procedure
    If Iter = "" Then Exit Sub
    'if the number entered is not between 1 and 1000, treat it as bad entry
    If (Iter < 1 Or Iter > 1000) Then GoTo BadEntry
    'if the number entered is valid, call reset function and start procudure
    'this part works fine
    Call Reset
    For i = 1 To Iter
    Application.Calculate
    Next i
    Exit Sub
    'if a bad entry found, take action
    BadEntry:
    'ask the user if they want to enter a valid number again
    Msg = "Your have entered an invalid number, you want to enter again?"
    'Here is the confusing part...when I test the code, the msgbox always pop up twice
    'even I choose yes...

    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?
    Last edited by dada540; 10-17-2010 at 12:39 AM.

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: how to limit user input between 1 to 1000

    Perhaps something like this
    Sub test()
        Dim uiValue As Long
        
        Do
            uiValue = Application.InputBox("Enter a value between 1 and 1000", Type:=1)
    
            If uiValue < 1 Then
                Rem cancel pressed
                Exit Sub
            End If
        Loop Until uiValue < 1001
        
        MsgBox uiValue
    End Sub
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Forum Contributor johnjohns's Avatar
    Join Date
    11-19-2007
    Location
    Dubai, UAE
    MS-Off Ver
    2003 and 2007
    Posts
    526

    Re: how to limit user input between 1 to 1000

    Also try the data validation. You can restrict the entries and also display input messages and error alerts.

    rgds

    johnjohns

+ 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