subroutine to calculate the sum of numbers in a row

mbababrik

Board Regular
Joined
Oct 19, 2010
Messages
76
ok im new to this wesite and a new user of VBA
I have a column of numbers, that I don’t know the length in advance, but is located in column A. I want to write a subroutine that calculates the sum of the following
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
Sum=1<SUP>st</SUP> number* last number+ 2<SUP>nd</SUP> number*next to last number + 3<SUP>rd</SUP> number* 3<SUP>rd</SUP> to last number etc. If the total number of numbers is odd, the middle number will have nothing to pair it with. In this case, square the middle number and add it to the sum. Note that the column contains only numbers and blank lines, and blank lines are found only after the column has ended.
i got the part on how to determine if the number is odd or even
Sub checkifevenorodd()
Dim userentry As Long
userentry = WorksheetFunction.CountA(Columns(1))
userentrydiv2 = CInt(userentry / 2)
userentrydiv2times2 = userentrydiv2 * 2
If userentrydiv2times2 = userentry Then
MsgBox ("even")
Else
MsgBox ("odd")
End If
End Sub
i appreciate any help
thank you
 
Just out of curiosity, how does the longer version that I posted fare against your data?
Don't get me wrong, assuming all else is equal, I'd go with what Mike posted, I'm just curious how it handles the even and odd number of values issue.
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I tested and found that if there are an even number of rows, MidValRo retains it's intial value of 0 and the second loops exits on the first pass.
Changing the second loop to
Code:
For Ro = 1 To LstRo [COLOR="Red"]/ 2[/COLOR]
  [COLOR="Red"]'[/COLOR]If Ro >= MidValRo Then Exit For
  TotalCount = TotalCount + Cells(Ro, "A") * Cells(LstRo, "A")
  LstRo = LstRo - 1
Next Ro
corrected that.
 
Upvote 0

Forum statistics

Threads
1,223,244
Messages
6,170,976
Members
452,372
Latest member
Natalie18

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