+ Reply to Thread
Results 1 to 6 of 6

sum function in VBA

  1. #1
    Registered User
    Join Date
    11-02-2011
    Location
    Houston, Texas
    MS-Off Ver
    Excel 2010
    Posts
    52

    sum function in VBA

    I need a sum function that takes data from a matrix and sums certain row tother

    Example)
    Data is in cells A1,B1,C1,D1,E1,F1
    and A2,B2,C2,D2,E2,F2

    I need the vba to in cell A3 sum A1 to C1
    then in cell B3 sum B1 to D1
    then in cell C3 sum C1 to E1

    then do the same thing for the next row beginning in Cell A4

    Please help!

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: sum function in VBA

    Do you mean VBA? Here's the simple formula which can be dragged across:
    Please Login or Register  to view this content.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    11-02-2011
    Location
    Houston, Texas
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: sum function in VBA

    Yes the amount of data I need to do this for is extrmely large (over 1 million cells) and causes excel to crash with the number of calculations and file size the formulas create. When I transition this to a vba formula the file size become much smaller.

    So far i have something like


    Range("B3").offset(i,k) = worksheetfunction.sum(Range("A1"), Range("A2") etc. but this only is resulting in the my matrix populating the same value for everything.

    If this makes sense.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: sum function in VBA

    I am perplexed by the idea that 1,000,000 simple SUM formulas would crash Excel? SUM is not a volatile function so each SUM would calc and settle, not causing further overhead. 1,000,000 one time calcs is not a problem for Excel.


    As for file size, maybe, that's a pretty small formula:

    =SUM(A1:C1)
    ...only nine characters, not that many more characters than if you just had the answers in those cells.

    But, you can still put the formulas in, then remove them, leaving the values behind:

    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  5. #5
    Registered User
    Join Date
    11-02-2011
    Location
    Houston, Texas
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: sum function in VBA

    Ended up solving it usinging some defined variables and nested offset functions

    note I predefined i, k, j

    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 07-25-2012 at 07:24 PM. Reason: Added code tags, as per forum rules. Don't forget!

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: sum function in VBA

    Glad you figured it out.

+ 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