Page 1 of 3 123 LastLast
Results 1 to 10 of 30

Thread: Vba loop

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

    Default Vba loop

    Hi guys,

    I need help with a VBA macro that Im supposed to finish for a course. The instructions for the macro is:

    Build a macro with a input box within a loop that asks the end user to put in numbers until the sum of numbers exceeds 100 then it ends and a message box appears which gives the sum of the numbers.

    How would one merits a macro like that?

  2. #2
    Board Regular
    Join Date
    Jan 2012
    Posts
    836
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Vba loop

    Do you know how to build a loop?

    Do you know how to terminate the loop?

    Do you know how to use the inputbox?

    Do you know how to use variables?

    Do you know how to increment the variable?

    If you know these things, the code should be simple=. If you don't, I suggest you go back to your class and tell them that you don't understand enough to complete the task, and seek help there?

    We are not a homework service.

  3. #3
    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 theBardd View Post
    Do you know how to build a loop?

    Do you know how to terminate the loop?

    Do you know how to use the inputbox?

    Do you know how to use variables?

    Do you know how to increment the variable?

    If you know these things, the code should be simple=. If you don't, I suggest you go back to your class and tell them that you don't understand enough to complete the task, and seek help there?

    We are not a homework service.
    I know how and I was just asking for help. I suspected getting a response like this. Thanks a lot 🙏

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

    Default Re: Vba loop

    If it was that simple you could just rather type the macro fast instead of writing such a long message

  5. #5
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    23,093
    Post Thanks / Like
    Mentioned
    390 Post(s)
    Tagged
    41 Thread(s)

    Default Re: Vba loop

    How far have you got with creating the macro?
    If you post what you have done so far, it would help.
    - 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

  6. #6
    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 Fluff View Post
    How far have you got with creating the macro?
    If you post what you have done so far, it would help.
    I’m sorry to disappoint but I haven’t even started creating it yet. This macro question has been asked in exams before, but there’s no clear solutions for it in the course material. It’s supposed to work as a bonus question in the exam so no big deal in that regard.

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

    Default Re: Vba loop

    As this is a homework question I am not going to supply the code, but will happily point you in the right direction if you get stuck.
    - 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. #8
    Board Regular
    Join Date
    Apr 2010
    Posts
    53
    Post Thanks / Like
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Vba loop

    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.***
    Last edited by Steve_; May 19th, 2019 at 02:08 PM.

  9. #9
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    23,093
    Post Thanks / Like
    Mentioned
    390 Post(s)
    Tagged
    41 Thread(s)

    Default Re: Vba loop

    @Steve_
    Nobody supplied the code as this is a homework question and most members prefer not do do other peoples homework, as it is a form of cheating.
    - 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

  10. #10
    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 Fluff View Post
    @Steve_
    Nobody supplied the code as this is a homework question and most members prefer not do do other peoples homework, as it is a form of cheating.

    I understand your position and i respect it. It is a totally moral, responsible, respectable, and commendable position to take. That being said, I on the other hand do not feel it is my place to determine or question how anyone does or does not do their homework. I only hope that my code is used by the requester to better their own understanding of the language. I provide the code. How they use it is up to them. I am not here to make moral determinations. I hope you can respect my position on the matter.

    This thread feels dangerously close to becoming off topic. I would prefer to focus on code.
    Last edited by Steve_; May 19th, 2019 at 02:13 PM.

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
  •