Question : Excel VBA - Use solver purely programmatically

I have seen some answers that partially address what I'm trying to get at (20299452 and 20357379).  However, this requires one to painstakingly figure out the API.  Can someone send me an example of a function that uses the xla solver code to figure out a result based on a sheet range passed into it.

Answer : Excel VBA - Use solver purely programmatically

Here is a code snippet that uses the Newton-Raphson method. I put '********** by the two statements that set either the upper or lower limit as you cut the range of the solution space in half with each iteration. Ten iterations puts the solutions in a 0.1% slice of the original range.

Brad
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
LowInventoryGuess = 0
        HighInventoryGuess = TankFull
        InitInv = LowInventoryGuess
        For j = 1 To 10
            StartInv.Value = InitInv
               'Calculate results, and return them to array X
            x = Perf(InputData, MilitaryHours, OKtoRun, BldgLoad, InputUnits, _
                    OutputUnits, MinInv, FinalInv, TotalProduction)
                'Exit sub if results meet goal: final inventory near initial
                'Capacity, MinInv & FinalInv all measured in input units
            FinalInv = FinalInv * (OutputEnergyFactor / InputEnergyFactor)
            MinInv = MinInv * (OutputEnergyFactor / InputEnergyFactor)
            InitialInventoryOK = FinalInv >= InitInv And FinalInv - InitInv <= Capacity
            MinimumInventoryOK = MinInv >= 0
    '
           'Display iteration counter & latest initial inventory on status bar
            GoalChk = Format(InitInv, "#,##0") & " InitInvOK " & InitialInventoryOK _
                & "   MinInvOK " & MinimumInventoryOK
            Application.StatusBar = "Iteration" & Str(i + j) & "   Starting Inventory=" & GoalChk
            If FinalInv >= InitInv Then
                LowInventoryGuess = InitInv          '*************
                InitInv = (HighInventoryGuess + LowInventoryGuess) / 2
            Else
                If j = 1 Then
                    'Convergence impossible. Restore screen updating, but leave status bar unchanged
                    Results.Value = x       'Update Results section with most recent calculation values
                    SmartFormatter          'Reformat the loads, capacity & inventory (if necessary)
                    Application.ScreenUpdating = True
                    Msg = "Problem either MaximICE/compressor limited or tank limited. Increase one or the other"
                    MsgBox Msg, vbExclamation
                    Exit Sub
                Else
                    HighInventoryGuess = InitInv               '************
                    InitInv = (HighInventoryGuess + LowInventoryGuess) / 2
                End If
Random Solutions  
 
programming4us programming4us