Whoops - sorry. Thought it looked a bit messy. Didn't know about code tag.
Full code follows.
Thoughts appreciated.
Sub MinCost()
ColOQ = 24 'Average Order
ColMult = 33 'Shipping Outer contains
ColMin = 25 'Min OQ
ColCost = 55 'Fully Loaded Cost
ColMax = 21 'Seasons sales
MaxIter = 5 ' Maximum Iterations
MinRow = 9 'First row that data in which data can be entered
MaxRow = 109 'Last row in which data can be entered
MinCOl = 3 'Min col for recalculate
MaxCol = 200 'maxCol for recalculate
' Parameters are Columns for Order Size, Multiple, Cell to minimise
Dim rng As Range
Dim lRowSelected As Long
' Application.Calculation = xlCalculationManual
For Each rng In Selection.Rows
lRowSelected = rng.Row
If lRowSelected >= MinRow And lRowSelected <= MaxRow Then
Multoq = Cells(lRowSelected, ColMult)
Minoq = Cells(lRowSelected, ColMin)
Maxoq = Cells(lRowSelected, ColMax)
Incoq = Max(Multoq, Int((Maxoq - Minoq) / MaxIter))
Incoq = Multoq * Int(Incoq / Multoq + 0.99999)
BestCost = 10 ^ 9
BestOQ = Minoq
OQ = Max(Incoq, Minoq) 'initial Order Quantity
Do While OQ < Maxoq
Cells(lRowSelected, ColOQ).Value = OQ
'Range("rng").Calculate 'recalculate values in this row
'Range(rng).Calculate
'Range(Cells(lRowSelected, MinCOl), Cells(lRowSelected, MaxCol)).Calculate
Calculate
If Cells(lRowSelected, ColCost) < BestCost Then
BestCost = Cells(lRowSelected, ColCost)
BestOQ = OQ
End If
OQ = OQ + Incoq
Cells(5, 3).Value = OQ
Cells(4, 3).Value = BestCost
Loop
Else
MsgBox "Please select a valid row before calculating the best OQ"
End If
Cells(lRowSelected, ColOQ) = BestOQ 'store answer
Next rng
Application.Calculation = xlCalculationAutomatic
' Calculate
End Sub
Bookmarks