Consulting

Results 1 to 14 of 14

Thread: Calculate power

  1. #1

    Calculate power

    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?

  2. #2
    VBAX Contributor GarysStudent's Avatar
    Joined
    Aug 2012
    Location
    Lakehurst, NJ, USA
    Posts
    127
    Location
    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!

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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

  4. #4
    This code would be great but I need this to be a simple macro to display the result in MsgBox

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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.

    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

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Quote Originally Posted by Pawel View Post
    This code would be great but I need this to be a simple macro to display the result in MsgBox
    MsgBox SumPowers (5, 2)
    ---------------------------------------------------------------------------------------------------------------------

    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

  7. #7
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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

  8. #8
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb()
       MsgBox [sumsq(1,2,3,4,5)]
    ' or
       MsgBox [sum({1,2,3,4,5}^2)]
    End Sub

  9. #9
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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

  10. #10
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    I waited for you to ask for it:

    Sub M_snb()
        x = 8
        y = 3
        
        MsgBox Evaluate("sum(row(1:" & x & ")^" & y & ")")
    End Sub

  11. #11
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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

  12. #12
    I know it sounded funny-simple to you guys but I'm jus a newbie trying to learn something
    Thanks for your help

  13. #13
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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

  14. #14
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Your question learned me that Excel contains the formula sumsq().

    Thank you

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •