Return Numerical Label for LAST value Subtracted to reach Sum Target Value

  • Thread starter Sam via OfficeKB.com
  • Start date
S

Sam via OfficeKB.com

Hi All,

I am looking for a Formula that can process the following:

The Sum Target Value is variable e.g.; 147
I have two columns of numerical values: Column “A” and Column “B.”
Column “A” Houses the Numerical Labels that I wish to have Returned when the
Sum Target Value is processed /reached - Subtract Sum Target Value as noted
below.

To Subtract Sum Target Value:
Start from LAST non-zero numerical value in Column “B” and Subtract one Cell
value at a time (or Sum up the Column) until the Sum Target Value or nearest
possible Sum BELOW, the Sum Target Value is reached. In this instance, it is
147. I wish to Sum the values in Column “B” to 147 per the above. The
summed values can be below BUT NOT over the Sum Target Value.

Return the Numerical Value that is Offset ONE Cell to the LEFT (Column “A”)
and ONE Row Above LAST value Subtracted (in Column “B”) to reach Sum Target
Value. The Result – Numerical Label should come from Column “A.”


Col “A” Col “B”
200 3
205 1
210 5
215 11
220 10
225 15
230 16
235 10
240 11
245 29
250 20
255 27
260 22
265 34
270 24
275 36
280 30
285 25
290 31
295 26
300 15
305 18
310 23
315 17
320 9
325 11
330 2
335 9
340 1
0 0

Reaching the Sum Target Value of 147 in Column “B” would go up to value 26,
Label 295 in Column “A”, totalling 131 which is below the Sum Target 147 but
including the value of 31 above it, would exceed the Sum Target Value of 147.
The required Result is returned from Column “A” Label 290 which is ONE Cell
to the LEFT (Column “A”) and ONE Row Above LAST value Subtracted (in Column
“B”).

Thanks
Sam
 
R

Ron Rosenfeld

Hi All,

I am looking for a Formula that can process the following:

The Sum Target Value is variable e.g.; 147
I have two columns of numerical values: Column “A” and Column “B.”
Column “A” Houses the Numerical Labels that I wish to have Returned when the
Sum Target Value is processed /reached - Subtract Sum Target Value as noted
below.

To Subtract Sum Target Value:
Start from LAST non-zero numerical value in Column “B” and Subtract one Cell
value at a time (or Sum up the Column) until the Sum Target Value or nearest
possible Sum BELOW, the Sum Target Value is reached. In this instance, it is
147. I wish to Sum the values in Column “B” to 147 per the above. The
summed values can be below BUT NOT over the Sum Target Value.

Return the Numerical Value that is Offset ONE Cell to the LEFT (Column “A”)
and ONE Row Above LAST value Subtracted (in Column “B”) to reach Sum Target
Value. The Result – Numerical Label should come from Column “A.”


Col “A” Col “B”
200 3
205 1
210 5
215 11
220 10
225 15
230 16
235 10
240 11
245 29
250 20
255 27
260 22
265 34
270 24
275 36
280 30
285 25
290 31
295 26
300 15
305 18
310 23
315 17
320 9
325 11
330 2
335 9
340 1
0 0

Reaching the Sum Target Value of 147 in Column “B” would go up to value 26,
Label 295 in Column “A”, totalling 131 which is below the Sum Target 147 but
including the value of 31 above it, would exceed the Sum Target Value of 147.
The required Result is returned from Column “A” Label 290 which is ONE Cell
to the LEFT (Column “A”) and ONE Row Above LAST value Subtracted (in Column
“B”).

Thanks
Sam

Here's one way that'll get you the correct answer. It involves adding a third
column:

C1: =SUM(B1:$B$30)

Then copy/drag down to C30.

The formula that will return the Label would then be:

=INDEX(A1:A30,MATCH(target_value,C1:C30,-1)+(COUNTIF(C1:C30,target_value)=0))


--ron
 
D

Domenic

Try the following...

First, define dynamic ranges for Column A and Column B...

Insert > Name > Define

Name: NumLabels

Refers to:

=Sheet1!$A$1:INDEX(Sheet1!$A:$A,MATCH(9.99999999999999E+307,Sheet1!$A:$A)
)

Click Add

Name: Values

Refers to:

=Sheet1!$B$1:INDEX(Sheet1!$B:$B,MATCH(9.99999999999999E+307,Sheet1!$A:$A)
)

Click Ok

Then use the following array formula that needs to be confirmed with
CONTROL+SHIFT+ENTER...

=INDEX(NumLabels,ROWS(Values)-MATCH(TRUE,SUBTOTAL(9,OFFSET(Values,ROWS(Va
lues)-1,0,-SMALL(ROW(Values)-MIN(ROW(Values))+1,ROW(Values)-MIN(ROW(Value
s))+1)))>=C1,0)+1)

....where C1 contains your 'Sum Target Value'.

Hope this helps!
 
R

Ron Rosenfeld

Here's one way that'll get you the correct answer. It involves adding a third
column:

C1: =SUM(B1:$B$30)

Then copy/drag down to C30.

The formula that will return the Label would then be:

=INDEX(A1:A30,MATCH(target_value,C1:C30,-1)+(COUNTIF(C1:C30,target_value)=0))


--ron


I misread your specifications. The correct formula would be:

=INDEX(A1:A30,MATCH(D1,C1:C30,-1))


--ron
 
S

Sam via OfficeKB.com

Hi Ron,

Thank you. I really do appreciate you taking the time and effort to provide a
workable solution to my often not so clear scenarios.

Ron said:
Here's one way that'll get you the correct answer. It involves adding a third column:
C1: =SUM(B1:$B$30)
Then copy/drag down to C30.
The formula that will return the Label would then be:
=INDEX(A1:A30,MATCH(target_value,C1:C30,-1)+(COUNTIF(C1:C30,target_value)=0))

Your Formula gets me very close to my required Result. It provides the
Numeric Label of the Summed Target Value; however, the required Result is the
Numeric Label ONE Row above the Numeric Label of the Summed Target Value. I
would have subtracted the value of one (1) from the Result returned by the
Formula but unfortunately, in a few cases the Numeric Label above the Summed
Target Value could be zero (0). Is there anyway of adding to your existing
Formula to return the Numeric Label that is one Row above the Summed Target
Value, excluding any Numeric Labels of zero (0) and thus returning what would
be the next non-zero Numeric Label.

Apologies for my over simplified example.

Further assistance appreciated.

Cheers
Sam
 
S

Sam via OfficeKB.com

Hi Domenic,

Thank you. Your time and assistance is very much appreciated.

Your Formula does provide exactly what I requested. Unfortunately, I over
simplified the example: in a few cases the Numeric Label above the Summed
Target Value could be zero (0). Is there anyway of adapting your existing
Formula to exclude any Numeric Labels of zero (0) and thus return what would
be the next non-zero Numeric Label.

Apologies for my over simplified example.

Further assistance appreciated.

Cheers,
Sam

Try the following...

First, define dynamic ranges for Column A and Column B...

Insert > Name > Define

Name: NumLabels

Refers to:

=Sheet1!$A$1:INDEX(Sheet1!$A:$A,MATCH(9.99999999999999E+307,Sheet1!$A:$A)
)

Click Add

Name: Values

Refers to:

=Sheet1!$B$1:INDEX(Sheet1!$B:$B,MATCH(9.99999999999999E+307,Sheet1!$A:$A)
)

Click Ok

Then use the following array formula that needs to be confirmed with
CONTROL+SHIFT+ENTER...

=INDEX(NumLabels,ROWS(Values)-MATCH(TRUE,SUBTOTAL(9,OFFSET(Values,ROWS(Va
lues)-1,0,-SMALL(ROW(Values)-MIN(ROW(Values))+1,ROW(Values)-MIN(ROW(Value
s))+1)))>=C1,0)+1)

...where C1 contains your 'Sum Target Value'.

Hope this helps!
[quoted text clipped - 58 lines]
Thanks
Sam
 
D

Domenic

Hi Sam!

No problem, see if this is what you're looking for...

D1:

=ROWS(Values)-MATCH(TRUE,SUBTOTAL(9,OFFSET(Values,ROWS(Values)-1,0,-SMALL
(ROW(Values)-MIN(ROW(Values))+1,ROW(Values)-MIN(ROW(Values))+1)))>=C1,0)+
1

....confirmed with CONTROL+SHIFT+ENTER

E1:

=LOOKUP(2,1/(A1:INDEX(NumLabels,D1)>0),A1:INDEX(NumLabels,D1))

Note that I've assumed that your data doesn't contain negative numbers.

Hope this helps!
 
S

Sam via OfficeKB.com

Hi Ron,
Ron Rosenfeld wrote:
I misread your specifications. The correct formula would be:
=INDEX(A1:A30,MATCH(D1,C1:C30,-1))

Your Formula does now provide exactly what I requested. Unfortunately, I over
simplified the example: in a few cases the Numeric Label above the Summed
Target Value could be zero (0). Is there anyway of adapting your existing
Formula to exclude any Numeric Labels of zero (0) and thus return what would
be the next non-zero Numeric Label.

Apologies for my over simplified example.

Further assistance much appreciated.

Cheers,
Sam


Ron said:
[quoted text clipped - 71 lines]

I misread your specifications. The correct formula would be:

=INDEX(A1:A30,MATCH(D1,C1:C30,-1))

--ron
 
S

Sam via OfficeKB.com

Hi Domenic,

That's Great!

Cheers,
Sam
D1:
=ROWS(Values)-MATCH(TRUE,SUBTOTAL(9,OFFSET(Values,ROWS(Values)-1,0,-SMALL
(ROW(Values)-MIN(ROW(Values))+1,ROW(Values)-MIN(ROW(Values))+1)))>=C1,0)+1
...confirmed with CONTROL+SHIFT+ENTER

Note that I've assumed that your data doesn't contain negative numbers.
 
D

Domenic

Hi Sam!

After looking at this again, I've come up with another formula to
replace the first one, which I believe is more efficient...

D1:

=ROWS(Values)-MATCH(TRUE,MMULT(--(LARGE(ROW(Values),ROW(Values)-MIN(ROW(V
alues))+1)<=TRANSPOSE(ROW(Values))),Values)>=C1,0)+1

....confirmed with CONTROL+SHIFT+ENTER.

Hope this helps!
 
R

Ron Rosenfeld

Hi Ron,



Your Formula does now provide exactly what I requested. Unfortunately, I over
simplified the example: in a few cases the Numeric Label above the Summed
Target Value could be zero (0). Is there anyway of adapting your existing
Formula to exclude any Numeric Labels of zero (0) and thus return what would
be the next non-zero Numeric Label.

Apologies for my over simplified example.

Further assistance much appreciated.

Cheers,
Sam

In your expansion you do not indicate how many zeros might be present, but
rather you ask to return the first non-zero label counting upwards.

I decided it would be simpler to write (and perhaps subsequently modify) a UDF.

The UDF accepts as arguments the Data table with numerical labels in the first
column and your values in the second column. The second argument will be the
Sum Target.

It should give the result you specify no matter how many 0's are present.

In addition, it will give a #NUM! error if the Sum Target cannot be reached.
You can certainly add other error conditions.

To enter this, <alt><F11> opens the VB Editor. Ensure your project is
highlighted inthe project explorer window, then Insert/Module and paste the
code below into the window that opens.

To use this, enter a formula of the type:

=label(A1:B30,D1)

where A1:B30 is your table of Numerical Labels and Data, and D1 is the Sum
Target.

===========================================
Option Explicit

Function Label(ByVal DataTable As Range, SumTargetValue As Range)
Dim Dt As Variant
Dim i As Long
Dim s As Double

Dt = DataTable

For i = UBound(Dt) To 1 Step -1
s = s + Dt(i, 2)
If s >= SumTargetValue Then Exit For
If i = 1 Then
Label = CVErr(xlErrNum)
Exit Function
End If
Next i

Do Until Label <> 0
Label = Dt(i, 1)
i = i - 1
Loop

End Function
=============================


--ron
 
D

Domenic

Shorter...

D1:

=ROWS(Values)-MATCH(TRUE,MMULT(--(ROWS(Values)-ROW(Values)+1<=TRANSPOSE(R
OW(Values))),Values)>=C1,0)+1

....confirmed with CONTROL+SHIFT+ENTER.

Hope this helps!
 
D

Domenic

For robustness...

D1:

=ROWS(Values)-MATCH(TRUE,MMULT(--(ROWS(Values)-(ROW(Values)-MIN(ROW(Value
s))+1)+1<=TRANSPOSE(ROW(Values)-MIN(ROW(Values))+1)),Values)>=C1,0)+1

....confirmed with CONTROL+SHIFT+ENTER.

Hope this helps!
 
S

Sam via OfficeKB.com

Hi Ron,

Thank you for providing this neat Function. Unfortunately, it is returning
#Value Error; any ideas?
To use this, enter a formula of the type:

where A1:B30 is your table of Numerical Labels and Data, and D1 is the Sum Target.
===========================================
Option Explicit

Function Label(ByVal DataTable As Range, SumTargetValue As Range)
Dim Dt As Variant
Dim i As Long
Dim s As Double

Dt = DataTable

For i = UBound(Dt) To 1 Step -1
s = s + Dt(i, 2)
If s >= SumTargetValue Then Exit For
If i = 1 Then
Label = CVErr(xlErrNum)
Exit Function
End If
Next i

Do Until Label <> 0
Label = Dt(i, 1)
i = i - 1
Loop

End Function
=============================

Cheers,
Sam
 
S

Sam via OfficeKB.com

Hi Domenic,

Thank you so much for your endeavours.
For robustness...
D1:
=ROWS(Values)-MATCH(TRUE,MMULT(--(ROWS(Values)-(ROW(Values)-MIN(ROW(Value
s))+1)+1<=TRANSPOSE(ROW(Values)-MIN(ROW(Values))+1)),Values)>=C1,0)+1
...confirmed with CONTROL+SHIFT+ENTER.

Cheers,
Sam

For robustness...

D1:

=ROWS(Values)-MATCH(TRUE,MMULT(--(ROWS(Values)-(ROW(Values)-MIN(ROW(Value
s))+1)+1<=TRANSPOSE(ROW(Values)-MIN(ROW(Values))+1)),Values)>=C1,0)+1

...confirmed with CONTROL+SHIFT+ENTER.

Hope this helps!
Shorter...
[quoted text clipped - 6 lines]
Hope this helps!
 
S

Sam via OfficeKB.com

Hi Ron,

UDF is working Great!

Found my problem: cell returning empty text.

Thank you so much for your assistance.

Cheers,
Sam
 
R

Ron Rosenfeld

Hi Ron,

Thank you for providing this neat Function. Unfortunately, it is returning
#Value Error; any ideas?

Possibly an entry in the Values column that is text and does not look like a
number.

If this is an issue, we can add some debugging code to try to sort it out.


--ron
 
R

Ron Rosenfeld

Hi Ron,

UDF is working Great!

Found my problem: cell returning empty text.

Thank you so much for your assistance.

Cheers,
Sam

Glad it is working. Thanks for the feedback.
--ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top