PDA

View Full Version : Solved: Calling the Solver from VBA



Saladsamurai
12-07-2009, 10:13 AM
Alright. I have a super simple Solver example in the attached workbook. I am solving the Equation 4*X=8.

I have the initial guess of X = 1 in cell A2 and I have Cell B2 = 4*A2.

I turned on the Macro recorder and solved the equation. I did not use any of the options. This is just a 'benchmark' to get things going. The following code was generated:
Option Explicit
Sub Solver1()
'
' Solver1 Macro
' Macro recorded 12/7/2009 by Saladsamurai


SolverOk SetCell:="$B$2", MaxMinVal:=3, ValueOf:="8", ByChange:="$A$2"
SolverSolve
End Sub


After viewing the code, I went back and changed the value of A2 (the initial guess) back to 1. I then went into the VBE and clicked on the http://i12.photobucket.com/albums/a220/saladsamurai/Play.jpg button.

I get the error message:

http://i12.photobucket.com/albums/a220/saladsamurai/errrrrr.jpg

Could someone explain to me what I am missing?

Thanks,
Casey

EDIT: WrkBk attached

Bob Phillips
12-07-2009, 11:14 AM
You need to set a reference to SOLVER in the VBIDE.

Saladsamurai
12-07-2009, 11:22 AM
You need to set a reference to SOLVER in the VBIDE.

Ok then :) Honestly, I have no idea what that means. What does 'setting a reference in the VBIDE' entail?

Thank!

Bob Phillips
12-07-2009, 12:07 PM
Go to the VBIDE (Alt-F11)

Look for the menu bar at the top of the window, there should be an item called Tools on that menu bar

Navigate your mouse over Tools and click it

The first item in that menu should be References, move your mouse over that and click it

You will see a check listbox, scroll down that list until you find an entry entitled SOLVER, then move your mouse over the checkbox square and click it (You should see a tick mark appear)

Exit out

Marcster
12-07-2009, 12:13 PM
In the Visual Basic Intergrated Development Environment (VBIDE),
where the VBA (Macro) code is.
Menu option:
Tools > References
In the Available References, tick SOLVER
Then OK

Saladsamurai
12-07-2009, 12:56 PM
xld, thank you for your painfully explicit instructions. Unfortunately (for me of course), I do not see SOLVER as one of the available references. I have the SOLVER as an add-in (obviously), so I am not sure what the next step is.

Saladsamurai
12-07-2009, 01:52 PM
Ok! I found it: Tools->References->Browse->C:\Program Files\Microsoft Office\OFFICE11\Library\SOLVER

For excel 2003