views:

888

answers:

4

I've got a spreadsheet like this:

date | 7/1 | 7/2 | 7/3 | 7/4
-----|-----|-----|-----|-----
 val |  3  |  5  |  1  |  3  
-----|-----|-----|-----|-----

I want to sum the val row, but only up to the current date. So if today were 7/3, the sum would be 3+5+1=9. If today were 7/4, it would be 12.

I figured this out to get the number of columns:

=YEARFRAC(B1,TODAY())*360  // B1 is the first date -- 7/1

but I can't figure out how to tell excel to do the sum:

=SUM(B2:<B+num cols above>2)

Presumably its something to do with references, and lookup, but I'm not really familiar with how those work....

A: 

The OFFSET function should do the job. There's a similar question here. Without knowing the exact layout of your table, I guess your formula would look something like:

=SUM(OFFSET(B2,0,0,1,DAY(TODAY())))

Where DAY(TODAY()) returns the day in the month. This number is then used as the width of the range to SUM over in OFFSET.

I hope that was relatively clear. Good luck.

anderstornvig
this also works:=SUM(OFFSET(C2,0,0,1,YEARFRAC(C1,TODAY())*360+1))(can't count on the days corresponding to the number of offsets)but it's more complex...
sprugman
+3  A: 

You can use SUMIF:

=SUMIF(A1:E1,"<="&TODAY(),A2:E2)

Assuming your dates are in a1:e1 and your values are in a2:e2.

edoloughlin
A: 

Put this VBA in a module

Option Explicit

Public Function GetTotal(StartCell As Range) As Integer 
    Dim i As Integer, j As Integer

    i = StartCell.Row
    j = StartCell.Column

    If Cells(i, j) > Date Then
        GetTotal = 0
        Exit Function
    End If

    While Cells(i, j) <> DateAdd("d", 1, Date) 'values up to an including today'
        GetTotal = GetTotal + Cells(i + 1, j)
        j = j + 1
    Wend

End Function

and then use it in a worksheet cell by inserting

=GetTotal([starting date cell])

where [starting date cell] is the cell with the date from which you want the sum. The worksheet cell's value will be the sum

Russ Cam
A: 

Second on the sumif. Defining your own vba function is an option but I usually reserve something like that for when there's no other options.

Dcritelli
I'm not really much of a stickler for this, but fwiw, this seems more like it should be comments on the sumif and vba suggestions (with votes) than an answer....
sprugman
I tried commenting but it said I didn't have enough rep to comment on other people's posts. That's the only reason I made it a separate post.
Dcritelli
ah. makes sense....
sprugman