Finding coverage of the range

darius_s

New Member
Joined
Apr 9, 2012
Messages
5
Hi all,

I have got an interesting problem . I have got a sample Data below:

[TABLE="width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[TD]F[/TD]
[TD]G[/TD]
[TD]H[/TD]
[TD]I[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]1[/TD]
[TD]2[/TD]
[TD]4[/TD]
[TD]4[/TD]
[TD]1[/TD]
[TD]2[/TD]
[TD]2[/TD]
[TD]3[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]13[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]5.5[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

I have got a target number in A2. I need to find how much of the range A1:I1 that number would cover. Each cell in the range is a day, and the value is sales per that day.

In the example, the coverage is 5.5 days (A3 cell) as I need A1:E1 and half of F1 to match the number.

I would have a few thousand calculations in the sheet, finding coverage for different products for stock levels in various points of time.

Currently I am using IFS function, that checks all of the scenarios (will stock cover 1 day -> what about 2 -> and 3 ->and 4 -> and so on...).

The current approach is working, but makes the entire calculation massive as I have more than 15 if conditions.

Multiply all that by a thousand or so of formula instances and we bordering unacceptable performance levels.

I was wondering if anyone has encountered something similar and has found a more elegant solution. :)
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Place this in module code...
Code:
Public Function CoverDays(Rng As Range, Result As Integer) As Double
'=Coverdays(A1:I1,A2)
Dim R As Range, Tot As Double, Temp As Double, Cnt As Integer
For Each R In Rng
Tot = Tot + R.Value
If Tot > Result Then
CoverDays = (Result - Temp) / R.Value + Cnt
Exit Function
End If
Temp = Tot
Cnt = Cnt + 1
Next R
End Function
To operate with the data as shown. Place this formula in A3...
Code:
=Coverdays(A1:I1,A2)
HTH. Dave
 
Upvote 0
Hi Dave,

Thanks for pointing to a direction of custom formulas.

Neat for sure. I will see what are the results performance wise.
 
Upvote 0
I did some testing. I had to declare results as "double", as other vise i was getting an error. Potentially integer overflow.

Performance wise, custom function has a slight performance penalty. Unfortunately I will have to live with massive formula.

But it was a very good try and definitely something learned :D
 
Upvote 0
If you are looking for performance, then ditch the IFS formula & replace it with a nested IF formula instead.
The new IFS function will evaluate the entire formula, whereas the old style IF function will only evaluate the true or false
 
Upvote 0
Maybe something like this...


[Table="class: grid"][tr][td="bgcolor: #DCE6F1"][/td][td="bgcolor: #DCE6F1"]
A
[/td][td="bgcolor: #DCE6F1"]
B
[/td][td="bgcolor: #DCE6F1"]
C
[/td][td="bgcolor: #DCE6F1"]
D
[/td][td="bgcolor: #DCE6F1"]
E
[/td][td="bgcolor: #DCE6F1"]
F
[/td][td="bgcolor: #DCE6F1"]
G
[/td][td="bgcolor: #DCE6F1"]
H
[/td][td="bgcolor: #DCE6F1"]
I
[/td][/tr]
[tr][td="bgcolor: #DCE6F1"]
1
[/td][td="bgcolor:#D9D9D9"]
1​
[/td][td="bgcolor:#D9D9D9"]
3​
[/td][td="bgcolor:#D9D9D9"]
7​
[/td][td="bgcolor:#D9D9D9"]
11​
[/td][td="bgcolor:#D9D9D9"]
12​
[/td][td="bgcolor:#D9D9D9"]
14​
[/td][td="bgcolor:#D9D9D9"]
16​
[/td][td="bgcolor:#D9D9D9"]
19​
[/td][td="bgcolor:#D9D9D9"]
23​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
2
[/td][td]
1​
[/td][td]
2​
[/td][td]
4​
[/td][td]
4​
[/td][td]
1​
[/td][td]
2​
[/td][td]
2​
[/td][td]
3​
[/td][td]
4​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
3
[/td][td]
Target​
[/td][td]
Result​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
4
[/td][td]
4​
[/td][td]
2,25​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
5
[/td][td]
13​
[/td][td]
5,5​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
6
[/td][td]
6​
[/td][td]
2,75​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
7
[/td][td]
7​
[/td][td]
3​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
8
[/td][td]
14​
[/td][td]
6​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
9
[/td][td]
2​
[/td][td]
1,5​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
10
[/td][td]
1​
[/td][td]
1​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
11
[/td][td]
19​
[/td][td]
8​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
12
[/td][td]
20​
[/td][td]
8,25​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
13
[/td][td]
23​
[/td][td]
9​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
14
[/td][td]
25​
[/td][td]
NA​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
15
[/td][td]
3​
[/td][td]
2​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]
[/table]


Data in A2:I2

Helper row (gray area)
Formula in A1 copied across
-SUM($A2:A2)

Target in A4:A...

Formula in B4 copied down
=IF(A4>I$1,"NA",MATCH(A4,A$1:I$1)+(A4-INDEX(A$1:I$1,MATCH(A4,A$1:I$1)))/IFERROR(INDEX(A$2:I$2,MATCH(A4,A$1:I$1)+1),I$2))

Hope this helps

M.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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