Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 30

Thread: Vba loop

  1. #11
    New Member
    Join Date
    May 2019
    Posts
    15
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Vba loop

    Quote Originally Posted by Steve_ View Post
    Code:
    Sub Loop_Test()
        Do Until iTotal > 100
            i = CInt(InputBox("Enter a number.", "Current Total = " & iTotal))
            iTotal = iTotal + i
        Loop
        MsgBox "Your final total is: " & iTotal, vbOKOnly, "Final Total"
    End Sub
    For the record, your original post was worded so that we assumed you simply wanted help accomplishing this yourself. Which is why nobody had provided the code for you at first. I think you had experienced coders eager to teach you to do this yourself. This is your homework after all. That being said, this accomplishes your task.

    I really do hope you take the time to understand what was written for the sake of your education.
    And I encourage you to ask questions about how this code works. We will be happy to help.

    ***As another note, I am not sure if you have any 'stability' requirements for this. But that code is very unstable as written (for example, entering anything other than a number in the input box will cause an error without some kind of data validation catch.***
    Thank you so much Steve. that is very kind of you. I already understand the code and thank you again for your answer. I noticed your message a little late and already built up the following code myself,

    Sub UntilHundred()
    Do until x = 100
    X = x + 1
    Message = "Give a number "

    Userinput = InputBox(Message)

    Userinput = Val(Userinput)

    Do Until Validnumber = True

    MsgBox "Sum of the numbers is " & sum

    Loop

    End sub


    I know that the loop is not right but anyway. thanks again...!

  2. #12
    Board Regular
    Join Date
    Apr 2010
    Posts
    53
    Post Thanks / Like
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Vba loop

    Quote Originally Posted by Xoolmonster View Post
    Thank you so much Steve. that is very kind of you. I already understand the code and thank you again for your answer. I noticed your message a little late and already built up the following code myself,

    Sub UntilHundred()
    Do until x = 100
    X = x + 1
    Message = "Give a number "

    Userinput = InputBox(Message)

    Userinput = Val(Userinput)

    Do Until Validnumber = True

    MsgBox "Sum of the numbers is " & sum

    Loop

    End sub


    I know that the loop is not right but anyway. thanks again...!

    This is a great attempt! However, it is definitely not going to accomplish your homework task. Would you like to work with us on it and we can help you fix it? There are a couple mistakes that if we resolve can fix your code without changing it too much.
    Last edited by Steve_; May 19th, 2019 at 02:29 PM.

  3. #13
    New Member
    Join Date
    May 2019
    Posts
    15
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Vba loop

    Quote Originally Posted by Steve_ View Post
    This is a great attempt! However, it is definitely not going to accomplish your homework task. Would you like to work with us on it and we can help you fix it? There are a couple mistakes that if we resolve can fix your code without changing it too much.
    Sure, from what i understand i should define the variable X as the userinput is that right?

  4. #14
    New Member
    Join Date
    May 2019
    Posts
    15
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Vba loop

    No wait, 1 should be userinput

  5. #15
    New Member
    Join Date
    May 2019
    Posts
    15
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Vba loop

    Sub UntilHundred()

    Do until x > 100

    X = x + userinput

    Message = "Give a number "

    Userinput = InputBox(Message)

    Userinput = Val(Userinput)

    MsgBox "Sum of the numbers is " & sum

    Loop

    End sub

  6. #16
    Board Regular
    Join Date
    Apr 2010
    Posts
    53
    Post Thanks / Like
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Vba loop

    Quote Originally Posted by Xoolmonster View Post
    Thank you so much Steve. that is very kind of you. I already understand the code and thank you again for your answer. I noticed your message a little late and already built up the following code myself,

    Sub UntilHundred()
    Do until x = 100
    X = x + 1
    Message = "Give a number "

    Userinput = InputBox(Message)

    Userinput = Val(Userinput)

    Do Until Validnumber = True

    MsgBox "Sum of the numbers is " & sum

    Loop

    End sub


    I know that the loop is not right but anyway. thanks again...!

    Code:
    Sub UntilHundred()
        Do Until x = 100  ' This will only exit your loop if x = EXACTLY 100. So if it is 101,
                          ' the loop will continue. Forever.
       'Do Until x => 100 ' This will exit as soon as the number EQUALS 100 or is GREATER THAN 100.
            x = x + 1     ' This is not needed, however, it also guarentees the loop ends eventually
            Message = "Give a number " ' Not needed. Not wrong either.
            Userinput = InputBox(Message) ' This probably should have been userinput = InputBox("Give a number ")
                                          ' Still not wrong though. Just 'clunky' as it adds the uneeded line above.
            Userinput = Val(Userinput)    ' This works as a great way to validate the data.
            ' Now you need to write a line that adds Userinput to x
            Do Until Validnumber = True   ' This entire loop is not needed and will ALWAYS result in an
                                          ' infinite loop. Delete this.
                MsgBox "Sum of the numbers is " & Sum ' This should only be performed once.
                                                      ' & Sum should be replaced with & x
            Loop                          ' Not needed. Delete this too.
        Loop                              ' Move this above the MsgBox line.
    End Sub
    Last edited by Steve_; May 19th, 2019 at 02:46 PM.

  7. #17
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    22,906
    Post Thanks / Like
    Mentioned
    385 Post(s)
    Tagged
    41 Thread(s)

    Default Re: Vba loop

    You're almost there, but you need to move this line
    Code:
    X = x + userinput
    after the inputbox.
    Also the message box should be & x rather than & sum
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 2003 & 2013 on Win 7

  8. #18
    New Member
    Join Date
    May 2019
    Posts
    15
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Vba loop

    Quote Originally Posted by Steve_ View Post
    Code:
    Sub UntilHundred()
        Do Until x = 100  ' This will only exit your loop if x = EXACTLY 100. So if it is 101,
                          ' the loop will continue. Forever.
       'Do Until x => 100 ' This will exit as soon as the number EQUALS 100 or is GREATER THAN 100.
            x = x + 1     ' This is not needed, however, it also guarentees the loop ends eventually
            Message = "Give a number " ' Not needed. Not wrong either.
            Userinput = InputBox(Message) ' This probably should have been userinput = InputBox("Give a number ")
                                          ' Still not wrong though. Just 'clunky' as it adds the uneeded line above.
            Userinput = Val(Userinput)    ' This works as a great way to validate the data.
            Do Until Validnumber = True   ' This entire loop is not needed and will ALWAYS result in an
                                          ' infinite loop. Delete this.
                MsgBox "Sum of the numbers is " & Sum ' This should onl be performed once.
                                                      ' & Sum should be replaced with & x
            Loop                          ' Not needed. Delete this too.
        Loop                              ' Move this above the MsgBox line.
    End Sub
    Sub UntilHundred()

    Do Until x > 100

    x = x + userinput

    userinput = InputBox("Give a number ")

    Userinput = Val(Userinput)

    Loop

    MsgBox "Sum of the numbers is " & x

    End sub


    Something like this right?

  9. #19
    Board Regular
    Join Date
    Apr 2010
    Posts
    53
    Post Thanks / Like
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Vba loop

    Quote Originally Posted by Xoolmonster View Post
    Sub UntilHundred()

    Do until x > 100

    X = x + userinput

    Message = "Give a number "

    Userinput = InputBox(Message)

    Userinput = Val(Userinput)

    MsgBox "Sum of the numbers is " & sum

    Loop

    End sub
    Code:
    Sub UntilHundred()
        Do Until x > 100 ' Good.
            x = x + Userinput ' You need to move this down below userinput = val(userinput)
            Message = "Give a number " ' Good
            Userinput = InputBox(Message) ' Good
            Userinput = Val(Userinput) ' Good
            MsgBox "Sum of the numbers is " & Sum ' Move this outside of your loop.
                                                  ' Also, what is 'sum'? I think you meant x
        Loop
    End Sub
    Almost there. see above.

  10. #20
    New Member
    Join Date
    May 2019
    Posts
    15
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Vba loop

    Quote Originally Posted by Xoolmonster View Post
    Sub UntilHundred()

    Do Until x > 100

    x = x + userinput

    userinput = InputBox("Give a number ")

    Userinput = Val(Userinput)

    Loop

    MsgBox "Sum of the numbers is " & x

    End sub


    Something like this right?
    So in this case ´´x = x + userinput´´ stores the inputs given by the user and the program keeps asking the user for a number until the sum on the users inputs are bigger than 100

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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