+ Reply to Thread
Results 1 to 6 of 6

Sum Function for a dynamic array

  1. #1
    Registered User
    Join Date
    08-12-2011
    Location
    Pittsburgh, PA.
    MS-Off Ver
    Excel 2007
    Posts
    3

    Sum Function for a dynamic array

    Hi, I am trying to sum columns with varying amounts of rows. I thouht this would be a simple code to write, but I am getting #VALUE as the result. I'm new to VB so any help is appreciated!! Here is my code:

    Public Function sumfunction(inum, itemarray()) As Double

    For i = 1 To inum
    sumfunction = sumfunction + itemarray(i)
    Next

    End Function


    I tested this code by placing digits 1 through 6 in cells A1 through A6, then used the formula, sumfunction(6,A1:A6) in cell A7. I received a #VALUE. Any thoughts?


    Thank you!!!
    Last edited by Mordred; 08-15-2011 at 09:04 AM.

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Sum Function for a dynamic array

    Hello MeganB and welcome to the forum. Please take a moment to read the forum rules (located in my signature) and wrap your code in code tags as per Rule #3. Once you do that, someone will be able to help you.

    Thanks.
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Registered User
    Join Date
    08-12-2011
    Location
    Pittsburgh, PA.
    MS-Off Ver
    Excel 2007
    Posts
    3

    Sum Function for a dynamic array

    Hi, I am trying to sum columns with varying amounts of rows. I thouht this would be a simple code to write, but I am getting #VALUE as the result. I'm new to VB so any help is appreciated!! Here is my code:
    Please Login or Register  to view this content.
    I tested this code by placing digits 1 through 6 in cells A1 through A6, then used the formula, sumfunction(6,A1:A6) in cell A7. I received a #VALUE. Any thoughts?


    Thank you!!!

  4. #4
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Sum Function for a dynamic array

    You are not actually passing an array, you are passing a range. Change the declaration to:
    Please Login or Register  to view this content.
    and the code should be fine. The way you have it declared now, you would have to call it using:
    =sumfunction(6,{1,2,3,4,5,6})
    for example.
    Remember what the dormouse said
    Feed your head

  5. #5
    Registered User
    Join Date
    08-12-2011
    Location
    Pittsburgh, PA.
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Sum Function for a dynamic array

    Thank you so much, that did the trick!!! :-)

  6. #6
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Sum Function for a dynamic array

    Glad to help.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1