Hi guys ,
I'm trying to write a macro that will calculate sum of powers(Y) of values from 1 to X
for example :
for X = 5 and Y=2 the value should be 1^2 + 2^2+3^2+4^2+5^2
any idea how to do that?
Hi guys ,
I'm trying to write a macro that will calculate sum of powers(Y) of values from 1 to X
for example :
for X = 5 and Y=2 the value should be 1^2 + 2^2+3^2+4^2+5^2
any idea how to do that?
Perhaps the UDF:
Public Function SumPowers(X As Long, Y As Long) As Long Dim i As Long SumPowers = 0 For i = 1 To X SumPowers = SumPowers + i ^ Y Next i End Function
Have a Great Day!
Just an FYI.
SumPowers = 0 is redundant in that code.
I expect the student to do their homework and find all the errrors I leeve in.
Please take the time to read the Forum FAQ
This code would be great but I need this to be a simple macro to display the result in MsgBox
Shirley, you can figure that out.This code would be great but I need this to be a simple macro to display the result in MsgBox
BTW, this sounds like a homework assignment.
I expect the student to do their homework and find all the errrors I leeve in.
Please take the time to read the Forum FAQ
---------------------------------------------------------------------------------------------------------------------
Paul
Remember: Tell us WHAT you want to do, not HOW you think you want to do it
1. Use [CODE] ....[/CODE ] Tags for readability
[CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
2. Upload an example
Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
3. Mark the thread as [Solved] when you have an answer
Thread Tools (on the top right corner, above the first message)
4. Read the Forum FAQ, especially the part about cross-posting in other forums
http://www.vbaexpress.com/forum/faq...._new_faq_item3
Heh heh.
I expect the student to do their homework and find all the errrors I leeve in.
Please take the time to read the Forum FAQ
Sub M_snb() MsgBox [sumsq(1,2,3,4,5)] ' or MsgBox [sum({1,2,3,4,5}^2)] End Sub
Slick. I like the second line.
Now, with variables, please.
I expect the student to do their homework and find all the errrors I leeve in.
Please take the time to read the Forum FAQ
I waited for you to ask for it:
Sub M_snb() x = 8 y = 3 MsgBox Evaluate("sum(row(1:" & x & ")^" & y & ")") End Sub
Yowzer!
I expect the student to do their homework and find all the errrors I leeve in.
Please take the time to read the Forum FAQ
I know it sounded funny-simple to you guys but I'm jus a newbie trying to learn something
Thanks for your help
You're welcome.
Yeah, sometimes our humour bleeds over. Sorry 'bout that.
I expect the student to do their homework and find all the errrors I leeve in.
Please take the time to read the Forum FAQ
Your question learned me that Excel contains the formula sumsq().
Thank you