Vba loop

Xoolmonster

New Member
Joined
May 19, 2019
Messages
15
Hi guys,

I need help with a VBA macro that I’m 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?
 

Some videos you may like

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

theBardd

Rules violation
Joined
Jan 21, 2012
Messages
912
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.
 

Xoolmonster

New Member
Joined
May 19, 2019
Messages
15
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 ?
 

Xoolmonster

New Member
Joined
May 19, 2019
Messages
15
If it was that simple you could just rather type the macro fast instead of writing such a long message
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
40,837
Office Version
365
Platform
Windows
How far have you got with creating the macro?
If you post what you have done so far, it would help.
 

Xoolmonster

New Member
Joined
May 19, 2019
Messages
15
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
40,837
Office Version
365
Platform
Windows
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.
 

Steve_

Board Regular
Joined
Apr 28, 2010
Messages
167
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:

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
40,837
Office Version
365
Platform
Windows
@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.
 

Steve_

Board Regular
Joined
Apr 28, 2010
Messages
167
@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:

Watch MrExcel Video

Forum statistics

Threads
1,099,318
Messages
5,467,907
Members
406,558
Latest member
MattJC7

This Week's Hot Topics

Top