Hi, so I have a set of code that is supposed to insert a price into a cell, run solver to find the optimal amount of land to allocate to two crops, paste the results into another sheet and then run again with the next price. As the price of crop A goes up, it makes sense that the allocation to crop B would go down. This happens ok for the first few runs but then at random prices it drops way down then back up on the next price - this is not logical. It runs ok if I do it manually and run solver outside the macro. I wrote the solver part of the code by using 'Record macro' and running the solver once then copying the code into the rest of my macro.
Any suggestions would be most helpful.
the code is:
Solve_Brig Macro
'
Sheets("Data").Select
Dim nprice As Single
Dim ycol As Integer
nprice = Worksheets("Data").Cells(29, 2).Value
ycol = 3
Application.ScreenUpdating = False
For y = 1 To nprice
Sheets("Data").Select
ActiveSheet.Cells(28, ycol).Select
Selection.Copy
ActiveSheet.Cells(28, 2).Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
Sheets("Brig_OptN").Select
Range("B8").Select
ActiveCell.Formula = "0"
Range("b19").Select
ActiveCell.Formula = "0"
SolverOk SetCell:="$B$37", MaxMinVal:=1, ValueOf:="0", ByChange:="$B$19,$B$8"
SolverOptions MaxTime:=100, Iterations:=100, Precision:=0.000001, AssumeLinear _
:=False, StepThru:=False, Estimates:=1, Derivatives:=1, SearchOption:=1, _
IntTolerance:=5, Scaling:=False, Convergence:=0.0001, AssumeNonNeg:=True
SolverOk SetCell:="$B$37", MaxMinVal:=1, ValueOf:="0", ByChange:="$B$19,$B$8"
SolverSolve UserFinish:=True
Sheets("Results").Select
Range("B3:B21").Select
Selection.Copy
Do
ActiveCell.Offset(0, 1).Select
Application.ScreenUpdating = False
Loop Until ActiveCell.Value = ""
ActiveCell.Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
'ActiveSheet.Paste
Application.CutCopyMode = False
Application.ScreenUpdating = False
ycol = ycol + 1
Next y
End Sub
Bookmarks