Hi,
I have written a macro to open a temporary workbook, copy/paste data from a main workbook, and then run Solver on the pasted data in the temporary workbook. When the macro executes, I keep getting a Macro Error display box, which reads: "Macro error at cell: [SOLVER.XLAM]Excel4Functions!A25." The box has options to Halt, Step, Continue, and a grayed-out GoTo. If I manually run Solver, I do not get the error box. Is there a way to correct whatever is causing this or, at a minimum, prevent the error box from displaying. This is the code I am using:
I have written a macro to open a temporary workbook, copy/paste data from a main workbook, and then run Solver on the pasted data in the temporary workbook. When the macro executes, I keep getting a Macro Error display box, which reads: "Macro error at cell: [SOLVER.XLAM]Excel4Functions!A25." The box has options to Halt, Step, Continue, and a grayed-out GoTo. If I manually run Solver, I do not get the error box. Is there a way to correct whatever is causing this or, at a minimum, prevent the error box from displaying. This is the code I am using:
Code:
Sub SolverHandling()
Dim results
Dim newbook As Workbook
Set newbook = Workbooks.Add 'because of slowness issue with solver
With newbook 'create temp workbook for solver calc
.SaveAs fileName:="T.xlsx"
End With
'copy data from this workbook to temp workbook
ThisWorkbook.Activate
Worksheets("TblRateCalc").Select
Cells.Select
Selection.Copy
Workbooks("T.xlsx").Activate
ActiveSheet.Paste
Range("N8").Select
SolverOk SetCell:="$N$8", MaxMinVal:=2, ValueOf:="0", ByChange:= _
"$M$2:$M$5,$O$2:$O$5"
SolverAdd CellRef:="$M$6", Relation:=2, FormulaText:="1"
SolverAdd CellRef:="$O$6", Relation:=2, FormulaText:="1"
SolverAdd CellRef:="$M$5", Relation:=1, FormulaText:="1"
SolverAdd CellRef:="$M$4", Relation:=1, FormulaText:="1"
SolverAdd CellRef:="$M$3", Relation:=1, FormulaText:="1"
SolverAdd CellRef:="$M$2", Relation:=1, FormulaText:="1"
SolverAdd CellRef:="$O$5", Relation:=1, FormulaText:="1"
SolverAdd CellRef:="$O$4", Relation:=1, FormulaText:="1"
SolverAdd CellRef:="$O$3", Relation:=1, FormulaText:="1"
SolverAdd CellRef:="$O$2", Relation:=1, FormulaText:="1"
SolverAdd CellRef:="$M$2", Relation:=3, FormulaText:="0"
SolverAdd CellRef:="$M$3", Relation:=3, FormulaText:="0"
SolverAdd CellRef:="$M$4", Relation:=3, FormulaText:="0"
SolverAdd CellRef:="$M$5", Relation:=3, FormulaText:="0"
SolverAdd CellRef:="$O$2", Relation:=3, FormulaText:="0"
SolverAdd CellRef:="$O$3", Relation:=3, FormulaText:="0"
SolverAdd CellRef:="$O$4", Relation:=3, FormulaText:="0"
SolverAdd CellRef:="$O$5", Relation:=3, FormulaText:="0"
SolverOk SetCell:="$N$8", MaxMinVal:=2, ValueOf:="0", ByChange:= _
"$M$2:$M$5,$O$2:$O$5"
SolverOptions MaxTime:=30, Iterations:=30, Precision:=0.0001, AssumeLinear _
:=False, StepThru:=False, Estimates:=1, Derivatives:=1, SearchOption:=1, _
IntTolerance:=5, Scaling:=False, Convergence:=0.0005, AssumeNonNeg:=False
SolverOk SetCell:="$N$8", MaxMinVal:=2, ValueOf:="0", ByChange:= _
"$M$2:$M$5,$O$2:$O$5"
SolverOptions StepThru:=True
results = SolverSolve(True, "SolverIteration")
Select Case results
Case 0, 1, 2
' solution found, keep final values
SolverFinish KeepFinal:=1
Case 4
'Target does not converge
Range("M2").Select
ActiveCell.FormulaR1C1 = "0"
Range("M3").Select
ActiveCell.FormulaR1C1 = "0"
Range("M4").Select
ActiveCell.FormulaR1C1 = "0"
Range("M5").Select
ActiveCell.FormulaR1C1 = "1"
Range("O2").Select
ActiveCell.FormulaR1C1 = "0"
Range("O3").Select
ActiveCell.FormulaR1C1 = "0"
Range("O4").Select
ActiveCell.FormulaR1C1 = "0"
Range("O5").Select
ActiveCell.FormulaR1C1 = "1"
Case 5
'Solver could not find a feasible solution
Range("M2").Select
ActiveCell.FormulaR1C1 = "0"
Range("M3").Select
ActiveCell.FormulaR1C1 = "0"
Range("M4").Select
ActiveCell.FormulaR1C1 = "0"
Range("M5").Select
ActiveCell.FormulaR1C1 = "1"
Range("O2").Select
ActiveCell.FormulaR1C1 = "0"
Range("O3").Select
ActiveCell.FormulaR1C1 = "0"
Range("O4").Select
ActiveCell.FormulaR1C1 = "0"
Range("O5").Select
ActiveCell.FormulaR1C1 = "1"
Case Else
Range("M2").Select
ActiveCell.FormulaR1C1 = "0"
Range("M3").Select
ActiveCell.FormulaR1C1 = "0"
Range("M4").Select
ActiveCell.FormulaR1C1 = "0"
Range("M5").Select
ActiveCell.FormulaR1C1 = "1"
Range("O2").Select
ActiveCell.FormulaR1C1 = "0"
Range("O3").Select
ActiveCell.FormulaR1C1 = "0"
Range("O4").Select
ActiveCell.FormulaR1C1 = "0"
Range("O5").Select
ActiveCell.FormulaR1C1 = "1"
End Select
Range("M2:O5").Select
Selection.Copy
ThisWorkbook.Activate
Range("M2").Select
ActiveSheet.Paste
Workbooks("T.xlsx").Close False
Kill "C:\CIM\Reports\Forecasting\New\T.xlsx"
End Sub