+ Reply to Thread
Results 1 to 15 of 15

Summing a variable number of cells

  1. #1
    Forum Contributor
    Join Date
    12-06-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    201

    Summing a variable number of cells

    I want to sum a number of cells. However; The number of cells vary from one run to the next. Is there a way to make the formula sum for instance Column D from row 7 to last row with a value in it? The rows can grow on every run but not necessarily. Or do I have to sum from row 7 to row last every time?

  2. #2
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Summing a variable number of cells

    Hi,

    Providing your entries in column D are entirerly numerical, this will sum from D7 to whatever happens to be the last cell in that range containing a number:

    =SUM(D7:INDEX(D:D,MATCH(70^50,D:D)))

    Regards
    Click * below if this answer helped

    Advanced Excel Techniques: http://excelxor.com/

  3. #3
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: Summing a variable number of cells

    May be...

    =SUM(D7:D1048576)


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  4. #4
    Forum Contributor
    Join Date
    12-06-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    201

    Re: Summing a variable number of cells

    Sixthsense: This is how I'm doing it at the moment. Was just wondering if there was a way to sum only to where the records end. Trying to squeeze as much speed out of it as possible but just summing a 1000000 cells is probably still faster then first having to calculate where to stop and then to sum up to there as XOR suggested.

  5. #5
    Forum Contributor
    Join Date
    12-06-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    201

    Re: Summing a variable number of cells

    XOR: Not all entries in the column will be numeric but once the numbers start there will be just numeric or blank cells.

  6. #6
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Summing a variable number of cells

    But you seemed to indicate that it would always begin at D7, and that it was only the last cell which needed to be determined dynamically, which is what my formula does.

    Are you now saying that both the first and last range references need to be determined dynamically?

    Regards

  7. #7
    Forum Contributor
    Join Date
    12-06-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    201

    Re: Summing a variable number of cells

    My bad. Yes, it will start at the same row. Above that is column headings etc. But there could be blank cells and/or zero values in between.

  8. #8
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Summing a variable number of cells

    That shouldn't matter. Can you give an example of a data sample where my formula did not give you the expected result?

    Regards

  9. #9
    Forum Contributor
    Join Date
    12-06-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    201

    Re: Summing a variable number of cells

    Didn't say it wouldn't give the correct result. Just said it would probably be just as quick to sum the whole range as it would be to evaluate the formula with two additional functions Index() and Match() to determine where to stop summing. I'm trying to squeeze every second I can out of the processing time. It's a model that runs a loop calculating a bunch of data as if it's running every day. To complete one run (almost 3000 days worth of data) takes the model two days to finish so time is somewhat important.

  10. #10
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Summing a variable number of cells

    Ah, I see. Interesting question. Let me get some advice on this.

    Regards

  11. #11
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Summing a variable number of cells

    As long as the last number in the column is the end of the used range a sum of the entire column would be more efficient *if* the amount of data is always the same.

    However, if one time you have 10k rows of data and the next time you only have 50 rows of data then the dynamic range formula would probably be better overall.

    As soon as you have 10k rows of data that defines the used range even though at present you only have 50 rows of data.
    Biff
    Microsoft MVP Excel
    Keep It Simple Stupid

    Let's Go Pens. We Want The Cup.

  12. #12
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Summing a variable number of cells

    fwiw for 10 recalculations range p1:p10000
    K
    L
    M
    22
    =SUM(P:P)
    =SUM(P1:INDEX(P:P,MATCH(70^50,P:P)))
    23
    24
    calculated time
    0.00125
    0.00134
    25
    0.00138
    0.00169
    26
    0.00132
    0.00128
    27
    0.00144
    0.00168
    28
    0.00142
    0.0014
    29
    0.00125
    0.0014
    30
    0.00131
    0.0015
    31
    0.00147
    0.00137
    32
    0.00139
    0.00181
    33
    0.00132
    0.00147
    34
    average time to run
    0.001355
    0.001494
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  13. #13
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Summing a variable number of cells

    Nice and interesting analysis, Martin.

  14. #14
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Summing a variable number of cells

    There is calculation timer code here:

    http://msdn2.microsoft.com/en-us/library/aa730921.aspx

    Your mileage may vary depending on the configuration of your machine and versions of software used.

    I use that code frequently and have buttons on my toolbars so I can quickly run tests.

  15. #15
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,612

    Re: Summing a variable number of cells

    Named Range: Rows2Sum ===>
    Please Login or Register  to view this content.
    Then
    Please Login or Register  to view this content.
    Ben Van Johnson

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Summing a variable number of cells
    By rschammel in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-30-2013, 02:55 AM
  2. [SOLVED] Need help with summing a variable number of cells
    By loolala in forum Excel Formulas & Functions
    Replies: 14
    Last Post: 07-02-2013, 07:56 PM
  3. Summing up a variable range of cells
    By Nerdio in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 04-24-2013, 03:07 PM
  4. Summing a variable number of cells
    By lovi in forum Excel General
    Replies: 3
    Last Post: 09-06-2011, 07:55 PM
  5. summing cells from a variable number of rows
    By Onesimus Prime in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 02-01-2008, 02:10 PM

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