+ Reply to Thread
Results 1 to 3 of 3

how to limit user input between 1 to 1000

  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
    Please Login or Register  to view this content.
    'Ask user to enter a number between 1 to 1000"
    Please Login or Register  to view this content.
    'if nothing entered, stop the procedure
    Please Login or Register  to view this content.
    'if the number entered is not between 1 and 1000, treat it as bad entry
    Please Login or Register  to view this content.
    'if the number entered is valid, call reset function and start procudure
    'this part works fine
    Please Login or Register  to view this content.
    'if a bad entry found, take action
    Please Login or Register  to view this content.
    'ask the user if they want to enter a valid number again
    Please Login or Register  to view this content.
    'Here is the confusing part...when I test the code, the msgbox always pop up twice
    'even I choose yes...

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    -------------------------------------------------------------------------------------------------------------

    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
    Please Login or Register  to view this content.
    _
    ...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