Link to home
Start Free TrialLog in
Avatar of mcgerks
mcgerks

asked on

VBA solver add-in function

Hi,

I am working on VBA code in MS Project and I'm trying to add a reference to the Solver add-in to an excel sheet without manually having to add it by going to tools referneces. Everyting seems to work ok, the sover reference is added to the VBA project in excel, but VBA doesn't like the line:

with objExcel
.SolverOk SetCell:="$A$4", MaxMinVal:=3, ValueOf:="0", ByChange:="$A$3"
.SolverSolve
end wtih

I get the error message: Run-time error '438'. Object doesn't support this property or method.

I was wondering if there is any way of calling the solverOK function through MS project using an excel sheet as an object.

Here is the code I have:

Option Explicit
Dim objExcel As Object


Sub test()

Create_New_Instance_of_Excel
Workbook_Open
runMe

End Sub


Sub Create_New_Instance_of_Excel()

Set objExcel = CreateObject("Excel.sheet")
Exit Sub

End Sub

Sub Workbook_Open()

Dim SolverPath As String

objExcel.Application.Visible = True
objExcel.Application.Cells(1, 1).Value = "Opened"
MsgBox "Workbook Opened"

SolverPath = "C:\Program Files\Microsoft Office\Office\Library\Solver\Solver.xla"
objExcel.VBProject.References.AddFromFile SolverPath

objExcel.Application.Cells(1, 1).Value = "Solver Added"
MsgBox "Solver Added"


End Sub


Sub runMe()

MsgBox "runMe"

With objExcel
.SolverOk SetCell:="$A$4", MaxMinVal:=3, ValueOf:="0", ByChange:="$A$3"
.SolverSolve

.Application.Cells(1, 1).Value = "runMe"
End With

MsgBox "runMe end"

End Sub
ASKER CERTIFIED SOLUTION
Avatar of Brendt Hess
Brendt Hess
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cerebralpc
cerebralpc

mcgerks,
No comment has been added lately (141 days), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:

RECOMMENDATION: Award points to bhess1 http:#8170046

Please leave any comments here within 7 days.

-- Please DO NOT accept this comment as an answer ! --

Thanks,

cerebralpc
EE Cleanup Volunteer

https://www.experts-exchange.com/questions/20556582/VBA-solver-add-in-function.html