VBA - Find sum of 31st - 50th even numbers

Robinhoods

New Member
Joined
Mar 14, 2022
Messages
5
Office Version
  1. 365
Platform
  1. MacOS
This is my homework : find sum of 31st - 50th even numbers between 1-100 by using either do-while or do-until loop. Pls help me
 
While you cannot use this because it does not comply with your instructor's requirements to use a loop, you might find it interesting to know you do not need a loop at all. Beside encoding Gauss's formula that BSALV posted, you could also do this...
VBA Code:
Sum31stTo50thEven = Application.Sum(Application.Index([ROW(1:100)], [2*ROW(31:50)]))
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
... and neither of these is correct.
Sub Sum31stto50thEvenNumbers()
Dim loop_ctr As Integer
Dim even_number_counter As Integer
Dim sum As Integer

loop_ctr = 1
sum = 0

Do Until loop_ctr > 100
If (loop_ctr Mod 2 = 0) Then
even_number_counter = even_number_counter + 1
If (even_number_counter > 30 And even_number_counter <= 50) Then
sum = sum + loop_ctr
End If
End If

If (even_number_counter = 50) Then
Exit Do
End If

loop_ctr = loop_ctr + 1
Loop

MsgBox "Sum of 31st to 50th even numbers is : " & sum
End Sub
Is this correct?
 
Upvote 0
Result is 1620:
VBA Code:
Sub sumeven()
Dim loop_ctr&, even_ctr&, sum&
    Do
        even_ctr = loop_ctr * 2 + 31 * 2 ' start from 62 to 64,66,...
        sum = sum + even_ctr ' running sum
        loop_ctr = loop_ctr + 1
    Loop Until even_ctr >= 100
MsgBox "Sum of even number is: " & sum
End Sub
 
Upvote 0
Is this correct?
You tell me. Your code gives the answer 1,620. Is that the answer you expect?

From a presentation point of view, it's good practice to indent your code, e.g. as @bebo021999 has done. It makes your code easier to read, and easier to debug.

VBA Code:
Sub Sum31stto50thEvenNumbers()

    Dim loop_ctr As Integer
    Dim even_number_counter As Integer
    Dim sum As Integer
   
    loop_ctr = 1
    sum = 0
   
    Do Until loop_ctr > 100
        If (loop_ctr Mod 2 = 0) Then
            even_number_counter = even_number_counter + 1
            If (even_number_counter > 30 And even_number_counter <= 50) Then
                sum = sum + loop_ctr
            End If
        End If
       
        If (even_number_counter = 50) Then
            Exit Do
        End If
       
        loop_ctr = loop_ctr + 1
    Loop
   
    MsgBox "Sum of 31st to 50th even numbers is : " & sum
End Sub
 
Upvote 0
Is the correct answer 410?
That would be the positive numbers > 30 and <=50.

formulas not VBA but the formula may help.

ser is a named array of numbers 1 to 100

T202203a.xlsm
EFGHI
7410
8410410410410
2c
Cell Formulas
RangeFormula
E7E7=50/2*(50/2+1)-30/2*(30/2+1)
E8E8=25*(25+1)-15*(15+1)
G8G8=SUM(IF((Ser>30)*(Ser<51)*(MOD(Ser,2)=0),Ser))
H8H8=SUM(IF(MOD(ROW(INDIRECT(B1&":"&C1)),2)=0,ROW(INDIRECT(B1&":"&C1))))
I8I8=SUM(IF(MOD(ROW(INDIRECT(31&":"&50)),2)=0,ROW(INDIRECT(31&":"&50))))
 
Upvote 0
Ignore the above post. I went back to delete the formulas etc but didn't complete the edits in time. It is quite late.



The original poster can confirm which result the he requires. Is it 1620 or 410?
 
Last edited:
Upvote 0
The sum of the positive numbers > 30 to 100 is 1620.
gaus says 100*101/2 -30*31/2 = 5050-465 = 4585
gaus also says that the sum of all the numbers between 31 and 50 = 50*51/2 - 30*31/2.
Like i read the question, the sum of the 31st until the 50st even number = that formula * 2, like i said yesterday.
 

Attachments

  • images (2).jpg
    images (2).jpg
    4.5 KB · Views: 9
Upvote 0

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

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