Share via

Why error when using "Solve" in macro

Anonymous
2020-02-21T05:36:37+00:00

I have recorded the macro below. Two questions:

(1) Why do I get the message "Compilation error: Sub or Function not defined" when i run it?

(2) How do i make it repeat itself from column "C" to "X"?

Option Explicit

Sub Solvesinus()

'

' Solvesinus Macro

'

SolverOk SetCell:="$C$21", MaxMinVal:=2, ValueOf:=0, ByChange:="$C$22:$C$24", _

Engine:=1, EngineDesc:="GRG Nonlinear"

SolverOk SetCell:="$C$21", MaxMinVal:=2, ValueOf:=0, ByChange:="$C$22:$C$24", _

Engine:=1, EngineDesc:="GRG Nonlinear"

SolverSolve

SolverOk SetCell:="$D$21", MaxMinVal:=2, ValueOf:=0, ByChange:="$D$22:$D$24", _

Engine:=1, EngineDesc:="GRG Nonlinear"

SolverOk SetCell:="$D$21", MaxMinVal:=2, ValueOf:=0, ByChange:="$D$22:$D$24", _

Engine:=1, EngineDesc:="GRG Nonlinear"

SolverSolve

End Sub

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2020-02-21T07:48:42+00:00

Hi. 

(1)  In the vba editor, go to "Tools" / "References..."  and check "Solver".

This adds the Solver code stuff to Excel vba. 

(2)  Here's a simple way to do it.  No error checking, or any constraints.

My preference is to just reset Solver in a loop.

In a Minimization problem, "valueof" is not required.

Sub Demo()

    Dim Cell As Range

    For Each Cell In [C21:X21].Cells

        SolverReset

        SolverOk Cell.Address, 2, , Cell.Offset(1, 0).Resize(3, 1).Address, 1

        SolverSolve True

    Next Cell

End Sub

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful