Using a Do while loop, sum all the even numbers from 0 to the number entered.

king_bevo13

New Member
Joined
Oct 19, 2017
Messages
1
I need help writing a sub that asks the user for a positive integer less than 100. Using a Do While loop sum all the even numbers from 0 to the number entered. Display the results of this calculation in a MsgBox. Assume 0 is an even number. Check to be sure that the number is within the specified range and allow the user to correct the entry if it is not without having to re-run the problem.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try:
Code:
Sub Test()
    Dim response As String
    Dim x As Long
    Dim y As Long
    y = 0
    Do
      response = InputBox("Please enter a number greater than 0 and less than 100.")
      If response <> "" And response > 0 And response < 100 Then
            For x = 0 To response Step 2
                y = x + y
            Next x
            MsgBox y
            Exit Do
      Else
            MsgBox ("You must enter a number greater than 0 and less than 100.")
      End If
    Loop
End Sub
 
Upvote 0
@Fluff: I never thought of that. I posted after seeing your response. Hopefully, it isn't homework!
 
Upvote 0
Code:
Sub test()

Dim uInput As Integer
Dim Result As Integer
Dim cnt As Integer
Result = 0
cnt = 0
uInput = Application.InputBox("Enter an Even Integer <= 100")
    
    If uInput >= 0 And uInput <= 100 And uInput Mod 2 = 0 Then
        Do While cnt < uInput
            cnt = cnt + 2
            Result = Result + cnt
        Loop
    Else
        MsgBox "Invalid Entry", vbCritical + vbOKOnly
        Exit Sub
    End If
    
MsgBox Result, vbOKOnly
End Sub
 
Upvote 0
@mumps
To me, it just seems a tad odd that the OP is so specific about using a Do While Loop & also a MsgBox
 
Upvote 0
You do not need to use a loop to calculate the sum of the even numbers from 0 to some given even even number, you can calculate it directly...
Code:
[table="width: 500"]
[tr]
	[td]Sub SumEvenNumbers()
  Dim Num As Long
  Num = Application.InputBox("What even number do you want to sum up to?", Type:=1)
  If Num Mod 2 <> 0 Then
    MsgBox "That was not an even number!!!"
  Else
    MsgBox "Sum of even numbers up to " & Num & " = " & [B][COLOR="#0000FF"]Num * (Num + 2) / 4[/COLOR][/B]
  End If
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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