Excel

Solver Add-in Installation/Reference Through VBA

Ease of Use

Easy

Version tested with

97, 2000, 2002 

Submitted by:

chitosunday

Description:

This code use the workbook open event to activate the macro to automatically install the add-in and reference of solver. Take note that the solver.xla and dll is assumed to be installed during the installation of microsoft office in your computer. 

Discussion:

Many users do not know that they need to install the solver add-in before they can use excel solver. This code will automate the installation of solver to excel thereby making it more user friendly. This code also automate installation of reference to solver which is for VBA user Only. 

Code:

instructions for use

			

'Put this in thisworkbook Option Explicit Private Sub Workbook_Open() FindSolverexcel End Sub 'Put this in a module Option Explicit Sub FindSolverexcel() 'Search the add-in file in your computer and then try to install On Error Resume Next With Application.FileSearch .NewSearch .SearchSubFolders = True .Filename = "Solver.xla" .LookIn = Application.Path .Execute If .Execute > 0 Then If AddIns("Solver Add-in").Installed = False Then AddIns("Solver Add-in").Installed = True 'if not installed check the cause If Err.Number > 0 Then 'Cause is security restriction If Err.Number = 1004 Then MsgBox "Please check if your security setting do not allow you to use VBProject" _ & vbCr & "Click Tools, Macro, security" & vbCr _ & "trusted source check access to Visual Basic Project" & vbCr & "close and open the file again" _ & vbCr & "if there is no option then proceed" Err.Clear Exit Sub Else 'cause is just not in the add-in list yet and it try to reinstall again AddIns.Add(.FoundFiles(1)).Installed = True Err.Clear End If End If 'gives the message that installation is successful CreateObject("WScript.Shell").Popup "Solver successfully installed", 1, "Solver" End If Else MsgBox "Solver Not Installed In this Computer", vbCritical Exit Sub End If End With Dim i As Integer, x As Long 'checks if the solver is already reference in the vba tool x = ThisWorkbook.VBProject.References.Count For i = 1 To x If ThisWorkbook.VBProject.References(i).Name = "SOLVER" Then CreateObject("WScript.Shell").Popup "Reference aleady set", 1, "Solver" Exit Sub End If Next i 'this put the solver as reference in vba tool ThisWorkbook.VBProject.References.AddFromFile Application.LibraryPath _ & Application.PathSeparator & "SOLVER" & Application.PathSeparator & "SOLVER.XLA" CreateObject("WScript.Shell").Popup "Reference successfully installed", 1, "Solver" 'this just clear the solver bugs Application.Run "Solver.xla!auto_open" End Sub

How to use:

  1. If you want to install the add-in only you can use only the first code from on error resume next to end with
  2. If you want to install the add-in including the reference, you need the full code.
 

Test the code:

  1. Just open the file and it will automatically install the solver. Goto to tools , add-ins the solver add-in should be checked. in the vba , go to tools, reference and see if the solver is checked. If checked , installation is successful.
 

Sample File:

solverinstallvba.zip 7.39KB 

Approved by mdmackillop


This entry has been viewed 266 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express