Count Until Sum of Counted = Value

hockey390

Board Regular
Joined
Jun 10, 2010
Messages
60
What I'm trying to accomplish is counting how many numbers from column A (starting at A2 and going down) it takes until it adds up to the number in B1. Twist of difficulty is I want a fraction.. So this would result in 3.5. 1+4+3+(half of 2). If A5 was = 3, answer would be 3.3.

AB
19
21
34
43
52
67
78
85
94

<tbody>
</tbody>
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

NeonRedSharpie

Well-known Member
Joined
Jul 14, 2014
Messages
1,678
Code:
Function ADDUNTIL(rng As Range, value As Variant) As Variant

Dim count As Long


    For Each cell In rng
        ADDUNTIL = ADDUNTIL + 1
        count = count + cell.value
        Select Case count
            Case value
                MsgBox ADDUNTIL
                Exit Function
            Case Is < value
            Case Is > value
                ADDUNTIL = ADDUNTIL - 1
                ADDUNTIL = ADDUNTIL + 1 - ((count - value) / cell.value)
                Exit Function
        End Select
    Next cell


End Function


Here is a user defined function that will do what you want.
 
Upvote 0

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
20,911
Office Version
  1. 365
Platform
  1. Windows
Using native functions, maybe...

Code:
=SUMPRODUCT(--(SUBTOTAL(9,OFFSET(A2:A9,,,ROW(A2:A9)-ROW(A2)+1))<=B1))+LOOKUP(0,(SUBTOTAL(9,OFFSET(A2:A9,,,ROW(A2:A9)-ROW(A2)+1))-A2:A9)-B1,(B1-(SUBTOTAL(9,OFFSET(A2:A9,,,ROW(A2:A9)-ROW(A2)+1))-A2:A9))/A2:A9)

Actually, in case B1 is greater than or equal the sum of values in A2:A9, try...

Code:
=IF(B1>SUM(A2:A9),"N/A",IF(B1=SUM(A2:A9),COUNT(A2:A9),SUMPRODUCT(--(SUBTOTAL(9,OFFSET(A2:A9,,,ROW(A2:A9)-ROW(A2)+1))<=B1))+LOOKUP(0,(SUBTOTAL(9,OFFSET(A2:A9,,,ROW(A2:A9)-ROW(A2)+1))-A2:A9)-B1,(B1-(SUBTOTAL(9,OFFSET(A2:A9,,,ROW(A2:A9)-ROW(A2)+1))-A2:A9))/A2:A9)))

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,191,190
Messages
5,985,201
Members
439,947
Latest member
fabiannic

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
Top