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?
 
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?


Code:
Sub UntilHundred()
    Do Until x > 100    ' good
        x = x + Userinput ' move this below your userinput lines
                          ' this is causing the inputs to be added
                          ' on the next loop instead of the current
        Userinput = InputBox("Give a number ") ' good
        Userinput = Val(Userinput) ' good
    Loop
    MsgBox "Sum of the numbers is " & x ' good
End Sub
 
Last edited:
Upvote 0

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Sub UntilHundred()

Do Until x > 100

userinput = InputBox("Give a number ")

Userinput = Val(Userinput)

x = x + userinput

Loop

MsgBox "Sum of the numbers is " & x

End sub

like this?
 
Upvote 0
Sub UntilHundred()

Do Until x > 100

userinput = InputBox("Give a number ")

Userinput = Val(Userinput)

x = x + userinput

Loop

MsgBox "Sum of the numbers is " & x

End sub

like this?

This code will accomplish what you provided guidelines for. Well done.
 
Upvote 0
And just for fun, this is your code, condensed/simplified...


Code:
Sub UntilHundred()
    Do Until UserInput > 100
        UserInput = UserInput + Val(InputBox("Give a number ", "Current Total is: " & 0 + UserInput))
    Loop
    MsgBox "Sum of the numbers is " & UserInput, vbOKOnly + vbInformation, "Sum"
End Sub

If you look at it and dissect it, you should be able to see every line of code you wrote contained in there., minus the stuff using 'x'. The same things that you did with x were done in this code, but instead it was all done to the UserInput variable instead.
 
Last edited:
Upvote 0
And just for fun, this is your code, condensed/simplified...


Code:
Sub UntilHundred()
    Do Until UserInput > 100
        UserInput = UserInput + Val(InputBox("Give a number ", "Current Total is: " & 0 + UserInput))
    Loop
    MsgBox "Sum of the numbers is " & UserInput, vbOKOnly + vbInformation, "Sum"
End Sub

If you look at it and dissect it, you should be able to see every line of code you wrote contained in there., minus the stuff using 'x'. The same things that you did with x were done in this code, but instead it was all done to the UserInput variable instead.

Now i see what you mean, was a bit hard at first to dissect. sort of a more effective way of doing it. what industry are you in since your this familiar with VBA or code in general?
 
Upvote 0
Now i see what you mean, was a bit hard at first to dissect. sort of a more effective way of doing it. what industry are you in since your this familiar with VBA or code in general?

I am a 'Systems Engineer' in the IT field. My degree is in Network Administration. Most of my work experience is in Server support and Business Application Development. I use several languages (VBA, VBS, VB.Net, PERL, PS, C, C++, C#, T-SQL, and Python) to create integration solutions. My specialty is MS Office automation. It is my niche skill that sets me apart from most.

Most of my VBA was learned as a hobby when I set out to create a NES/SNES style RPG. It was a blast! And so fun! I never thought I would get that thing working. Music, Backgrounds, Combat system, save game progress, quest system, user NPC dialogue, stat driven switches. It is actually WHOLE lot of fun to play. My friends love it.
 
Last edited:
Upvote 0
I am a 'Systems Engineer' in the IT field. My degree is in Network Administration. Most of my work experience is in Server support and Business Application Development. I use several languages (VBA, VBS, VB.Net, PERL, PS, C, C++, C#, T-SQL, and Python) to create integration solutions. My specialty is MS Office automation. It is my niche skill that sets me apart from most.

Most of my VBA was learned as a hobby when I set out to create a NES/SNES style RPG. It was a blast! And so fun! I never thought I would get that thing working. Music, Backgrounds, Combat system, save game progress, quest system, user NPC dialogue, stat driven switches. It is actually WHOLE lot of fun to play. My friends love it.

Thats interesting, lots of languages. But from my understanding all languages follow the same logic more or less. You’re a pretty smart guy, accomplished a lot... I do this for my bachelors in economics, Helsinki Finland. Programming is essential to know as an economist nowadays.
 
Upvote 0
Thats interesting, lots of languages. But from my understanding all languages follow the same logic more or less. You’re a pretty smart guy, accomplished a lot... I do this for my bachelors in economics, Helsinki Finland. Programming is essential to know as an economist nowadays.

The syntax can vary wildly. Especially when I get stuck working with ancient languages I learned in the 90s (COBOL, Fortran, Pascal, GW/Q/Basic , BasicA.

But once a coder learns a good amount of VB, C, and maybe some Java or a .net langauge, most are able to bumble through most languages albeit slowly.
The biggest exception to this i can think of is T-SQL. That one is its own animal.
 
Last edited:
Upvote 0
The syntax can vary wildly. Especially when I get stuck working with ancient languages I learned in the 90s (COBOL, Fortran, Pascal, GW/Q/Basic , BasicA.

But once a coder learns a good amount of VB, C, and maybe some Java or a .net langauge, most are able to bumble through most languages albeit slowly.
The biggest exception to this i can think of is T-SQL. That one is its own animal.

I very much enjoyed talking to you Steve. I sent you a friendsrequest aswell. Hope to have future interesting discussions. I wish you good health for your upcoming new work projects what not!
 
Upvote 0

Forum statistics

Threads
1,213,482
Messages
6,113,913
Members
448,532
Latest member
9Kimo3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top