Vba loop

Steve_

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

Some videos you may like

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Xoolmonster

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

Steve_

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

Steve_

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

Xoolmonster

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

Steve_

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

Xoolmonster

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

Steve_

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

Xoolmonster

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

Watch MrExcel Video

Forum statistics

Threads
1,099,314
Messages
5,467,871
Members
406,558
Latest member
MattJC7

This Week's Hot Topics

Top