Can an Excel formula count until a certain sum is reached?

dunno in dallas

New Member
Joined
Apr 24, 2002
Messages
24
I have a two column array where the total row # is dynamic. Column A in the array consists of a date and column B consists of a number. Is there an Excel formula that can return the number of rows (starting at B1) required until the sum of the numbers in column B is say 3?

A B
09/01/05 1
09/02/05 0
09/03/05 0
09/04/05 1
09/05/05 1
09/06/05 1

The correct response here would be "5", as it takes 5 rows until the sum of 3 is reached. Thank you so much. -DID
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Ok, my formulaic solution requires a helper column. In column C make a running total of coulmn b. Then, put this formula where you want your result.

=MATCH(3,B1:B7,0), where B1:B7 would be replaced by your range.

You said your range is variable so I would suggest assigning a dynamic named range that will acomidate the entire set of numbers in C column.

If you can't use the hleper column let me know, I'll get a VBA solution that will work to.
 
Upvote 0
here is the VBA solution....

Code:
Sub total()
    Dim last_cell As Integer
    Dim total As Integer
    Dim i As Integer

    last_cell = Range("b65536").End(xlUp).Row

    i = 0
    Do Until total = 3 Or i = last_cell
        i = i + 1
        total = total + Cells(i, 2).Value
    Loop
    If total < 3 Then
        Range("c8").Value = "Never reached desired number."
    Else
        Range("c8").Value = i
    End If

End Sub
 
Upvote 0
One way...
Book1
ABCDE
19/1/20051
29/2/2005035
39/3/20050
49/4/20051
59/5/20051
69/6/20051
Sheet1


E2:

=MATCH(D2,SUBTOTAL(9,INDIRECT("$B$1"&":"&ADDRESS(ROW($B$1:$B$6),COLUMN($B$1)))),0)

which must be confirmed with control+shift+enter.
 
Upvote 0
Thanks for all 3 responses. Prefer a formula over VBA, and of the two formula responses, AA's solution does not require a "helper" column. Aladin, your formula rocks. Thanks a bunch. -DID
 
Upvote 0
This is a great formula, but is there a way to have it return the number of records for not an "exact match". For example if you used the same information above, but you wanted to get to 10.5 minutes, how could you get it to still return 12 (if it is ok to be short) or 13 (if it is ok to go over)?
 
Upvote 0
Hello and welcome to the board,

not sure where 10.5, 12 & 13 came from:confused:

see if this helps you
Excel Workbook
AB
1AB
201/09/20051
302/09/20050
403/09/20050
504/09/20051
605/09/20051
706/09/20051
Sheet12
Excel 2003
Excel Workbook
DE
63.56
Sheet12
Excel 2003
#VALUE!
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
 
Upvote 0

Forum statistics

Threads
1,223,948
Messages
6,175,578
Members
452,652
Latest member
eduedu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top