Excel Formula

hrayani

Well-known Member
Joined
Jul 23, 2010
Messages
1,502
Office Version
  1. 2016
Platform
  1. Windows
Hello Friends,

Please have a look at the below table...
Formula required for Column E & F

Any help would be appreciated

Book2
ABCDEF
1Container SizeM3M3Required ContainersTotal Containers
2[20]30301 [20]1
3[40-STD]50501 [40-STD]1
4[40-HC]65651 [40-HC]1
5951 [40-HC] + 1 [20]2
6961 [40-HC] + 1 [40-STD]2
71151 [40-HC] + 1 [40-STD]2
81291 [40-HC] + 1 [40-STD]2
91302 [40-HC]2
101352 [40-HC] + 1 [20]3
111602 [40-HC] + 1 [20]3
121612 [40-HC] + 1 [40-STD]3
13
1420 feet container can take a load of max 30 M3
1540-STD feet container can take a load of max 50 M3
1640-HC feet container can take a load of max 65 M3
Sheet1


Regards,
Humayun
 
Hi JGordon,

Just tried & found that it worked on any range... There is no stopping to that... Sorry I should have tested this before

Only thing remains is I want the code to look at the cell values for cost & capacity ... Just in case if there is a need to change the capacity or the cost then one would be able to change it on the sheet rather than opening the VBA editor...

I also noticed that if the cost is same for 40 STD & 40 HC then the UDF gives preference to 40 HC - rightly so :)
 
Upvote 0

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
This version takes three parameters: M3 as either a range reference or a number, Capacity of the three containers as a three cell range, and Cost to use each container as a three cell range. The Capacity and Cost ranges should be absolute references (example $B$1:$D$1) or a named range for example you could name one range Capacity and the Cost in the Formulas tab Name Manager. That is what I have done in the example below. As you've discovered you can put the formula in any 1 row by three column range and if provide the correct parameters you should get the correct result. Note that the capacity range needs to be in increasing order and the cost range needs to correspond to the capacity range. Also if the per unit cost of the large container is more than the per unit cost of a smaller container it won't provide optimal results because for run time efficiency the code assumes using the large container a lot for large orders and then optimizes the remaining 10 or so containers.

VBA Code:
Function OptimumCapacity(Quant As Double, myCapacity As Range, myCost As Range)
    If myCapacity.Cells.Count <> 3 Or myCost.Cells.Count <> 3 Then
        OptimumCapacity = "Invalid range"
        Exit Function
    End If
    Dim c, Containers(1 To 1, 1 To 3), MinCost As Double, OrderCost As Double, Num As Long, i As Long, j As Long, k As Long
    Dim Capacity(0 To 2), Cost(0 To 2), OrderQuant As Long
    MinCost = 9E+99
    i = 0
    For Each c In myCapacity
        Capacity(i) = c
        i = i + 1
    Next
    i = 0
    For Each c In myCost
        Cost(i) = c
        i = i + 1
    Next
    Do While Quant - Capacity(2) > Capacity(2) * 10
        Quant = Quant - Capacity(2)
        Num = Num + 1
    Loop
    For i = 0 To 10
        For j = 0 To 10
            For k = 0 To 10
                OrderCost = i * Cost(0) + j * Cost(1) + k * Cost(2)
                OrderQuant = i * Capacity(0) + j * Capacity(1) + k * Capacity(2)
                If OrderQuant >= Quant And OrderCost < MinCost Then
                    Containers(1, 1) = i
                    Containers(1, 2) = j
                    Containers(1, 3) = k + Num
                    MinCost = OrderCost
                End If
            Next
        Next
    Next
    OptimumCapacity = Containers
End Function

RandomQuestions.xlsm
ABCDEF
1Capacity255565
2Cost255565
3
4
5M3255565CapacityExcess Capacity
620100255
725100250
8302005020
9402005010
1050200500
1160001655
1270300755
1380110800
1490101900
151004001000
161100201100
171200111200
181300021300
191403011400
201506001500
211602201600
221702111700
231802021800
241901301900
252001212000
262101122100
272200402200
282300312300
292000082420000
303000183930000
3170000210670000
321000001153100000
Sheet4
Cell Formulas
RangeFormula
B6:D32B6=OptimumCapacity(A6,Capacity,Cost)
E6:E32E6=SUMPRODUCT(B6:D6,$B$5:$D$5)
F6:F32F6=E6-A6
Press CTRL+SHIFT+ENTER to enter array formulas.
Named Ranges
NameRefers ToCells
Capacity=Sheet4!$B$1:$D$1B6:D32
Cost=Sheet4!$B$2:$D$2B6:D32
 
Upvote 0
Solution

Forum statistics

Threads
1,216,028
Messages
6,128,399
Members
449,447
Latest member
M V Arun

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