+ Reply to Thread
Results 1 to 3 of 3

Using Do and For Loops to get an integer value from a user

Hybrid View

learning_vba Using Do and For Loops to get... 03-28-2010, 07:33 PM
Leith Ross Re: Using Do and For Loops to... 03-28-2010, 09:24 PM
learning_vba Re: Using Do and For Loops to... 03-28-2010, 09:28 PM
  1. #1
    Registered User
    Join Date
    03-28-2010
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    60

    Using Do and For Loops to get an integer value from a user

    Hello,

    This is my first post and I appreciate any tips that can be offered to me on the following problem I am trying to solve. I am learning VBA in a college course and have a homework problem I am having difficulty figuring out (I am brand new to VBA).

    Here is the setup:

    Write a sub that asks a user via inputbox for an integer value between 1 and 5. This should be embedded within a Do loop so that the user keeps getting asked to input a number until they input an integer between 1 and 5. Use a For Loop for error checking.

    This is not the exact problem I am tasked with completing, but it is similar and will help me to learn the particular coding techniques I need to know to complete the problem.

    Thank you again for any help.

    Regards.
    Last edited by learning_vba; 03-28-2010 at 09:29 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Using Do and For Loops to get an integer value from a user

    Hello learning_vba,

    Welcome to the Forum!

    As a general rule, we do not provide answers to homework questions. The reason is obvious. In this case I will make an exception because your assignment doesn't have anything to do with looping structures but with validation and conditional transfer of execution.

    This macro will transfer control back to the line label "GetInput" based a series of validation tests. If any validation step fails, execution returns back to this line to ask the user to input a value again. An error dialog will be displayed alerting the user to the mistake.
    Sub InputTest()
    
      Dim Answer As Variant
       
    GetInput:
        Answer = InputBox("Enter a number from 1 to 5.")
    
       'Did user click Cancel?
        If Answer = "" Then
          Exit Sub
        End If
        
       'Is input a number?
        If Not IsNumeric(Answer) Then
          MsgBox "Please enter a number from 1 to 5."
          GoTo GetInput
        End If
    
       'Convert Answer from a string to an Integer
        Answer = CInt(Answer)
    
       'Is number 1 to 5?
        If Answer < 1 Or Answer > 5 Then
          MsgBox "Please enter a number from 1 to 5."
          GoTo GetInput
        End If
    
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    03-28-2010
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    60

    Re: Using Do and For Loops to get an integer value from a user

    Thank you very much for your help. I understand not answering the actual questions, but this one is not the exact question I have to answer. I appreciate the help and this will certainly help me to further understand what I am doing.

    Regards.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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