Hi guys, (NEED HELP WITH VBA CODE IN EXCEL) It seems as if I cannot get the loop
ID: 3598446 • Letter: H
Question
Hi guys,
(NEED HELP WITH VBA CODE IN EXCEL)
It seems as if I cannot get the loop to work correctly. When the input is incorrect (number plugged in is less than zero or equal to or greater than 100), it still gives me the answer before looping back to the original question. How can I get it to loop to the original question without it answering for the incorrect input? Can you correct this code for me please.
Thank you in advance.
(General) v(Declarations) ption Explicit Sub DisplaySumEven () Dim uservalue As Integer Dim total As Integer Dim i As Integer Do uservalueInputBox ("Enter a positive integer less than 100") If uservalue >= 0 And uservalueExplanation / Answer
Explanation: Atfer taking input, just check that input with while loop and then check the input is valid or not in if loop, then it works correctly.
Code:
option Explicit
Sub DisplaySumEven()
Dim userValue as Integer
Dim Total as Integer
Dim i as Integer
Do
userValue = InputBox("Enter a positive integerless than 100")
Do while i<userValue
If userValue>=0 and userValue<100 then
i=i+1
If i mod 2 = 0 then
total = total+i
End if
End if
Else
Msgbox"Invalid input,you must enter a positive integer less than 100", vbCritical+vbOkOnly
Loop
msgbox" "& usIp & " " & Total,vbInformation
Loop
end Sub
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.