VBA and Excel Solver
VBA and Excel Solver
Why VBA?
Visual Basic for Applications (VBA) is Excels programming language to develop applications in Excel. Details of the spreadsheet models you develop may be incomprehensible for less-technical people in your organization It will be helpful to develop a user-friendly application with a front end and a back-end.
The front end will present the user with dialog boxes to provide inputs of the model. The back end is a nontechnical report of the results.
Download excel model file of Wyndor Glass problem from ICON. Save the file as an Excel Macro-Enabled Workbook (.xlsm) file.
To add: go to FileOptionsCustomize Ribbon: check Developer in Main Tabs and click OK. Click the Macro Security icon to make sure Disable all macros with notification is selected under Macro Settings. When you work on VBA and see a security warning about Macros, select Enable Macros. Click the Visual Basic icon on the developer tab (or press Alt-F11) to open the Visual Basic Editor (VBE). Go to ToolsOptions menu item of VBE and check the Require Variable Declarations box.
4
In VBE, select the project (WyndorGlass.xlsm) in the project explorer window. InsertUserForm Select the new form. In the Properties window, set
Project explorer
Resize the form. Add the following controls from the Toolbox: Label, Command-Button, Frame, and TextBox. You can change the Font of the caption of the control in the Properties window. Use Ctrl + drag to duplicate a control. The two buttons are named btnOK and btnCancel. The five textboxes are named txtDoor, txtWindow, txtPlant1, txtPlant2, and txtPlant3.
Select frmInputs in the Project explorer. Click ViewCode. (Use ViewObject if you want to see the form design) Need codes to handle the following events: btnCancel_Click, btnOK_Click, and UserForm_Initialize. Codes for btnCancel_Click():
First define two ranges in the Model sheet named UnitProfits and ProductionHours, which are the coefficients of the objective function and the right hand sides. The codes store user inputs to the corresponding ranges in the model sheet.
Start by With and end by End With It could save a lot of typing. For example,
is equivalent to
Range(UnitProfits).Cells(i) is the ith cell in the range UnitProfits. Ranges can also be specified as Range (A1:C3) or Range(A1). The Val function converts user responses (treated as strings) to numerical numbers.
Adding the following codes to the beginning of the function btnOK_Click() will check, when the OK button is clicked, if the entered values are numerical, nonempty, and nonnegative.
10
Explanation of Codes
For Each loops: used when you want to loop through each object (control, cell, worksheet) in a collection (Controls, Range, Worksheets). The typical form of a For Each loop is:
Dim item as Control For Each item in Me.Controls Statements Next
11
These codes are run when the user form first appears. The codes initialize the text boxes with the current values in the Model sheet.
12
Codes are needed to show the user form frmInputs and run the Solver. To add codes other than event handlers, a module should be added by InsertModule (first select correct project in the Project explorer window).
13
Explanation of Codes
Option Explicit: automatically added when Require Variable Declaration is checked. It requires declaring all variables. Dim [Variable Name] As [Variable Type]: declaration of variables. Variable types can be Integer, String, Boolean, Double, and so on. frmInputs.Show show the user form frmInputs. Worksheets(Model).Activate activates the Model worksheet. SolverSolve function runs the Solver. To use it, a reference to the Solver add-in should be set in the VBE: ToolsReferences, then check box for SOLVER. When SolverSolve is used with the argument UserFinish:=True, then the solver results dialog box below will not be shown.
14
SolverSolve function returns an integer value that indicates Solvers outcome, where 5=infeasible, and 4=unbounded. If Constructions: handle conditions
If-Then-Else-End If:
If condition Then Statements 1 [Else Statements 2] End If
MsgBox [Message string], [button style], [title string] vbInformation is a built-in VBA constant that inserts an i icon in the message box. _: continue to next line
15
Introduction Sheet
Contains introduction information of the application and a button to start the main sub. Add an new sheet named Introduction. To add a button on the sheet, under Developer tab of the Excel file, click Insert. Add the button on the upper-left corner, link it to the MainWyndor sub, and change caption of the button. To make sure the Introduction sheet is shown every time the Excel file is opened, select ThisWorkbook in the Project explorer window and ViewCode. Add the Workbook_Open sub and add this code to the sub: Worksheets("Introduction").Activate
16
Online help tool: Object Browser on the standard toolbar of the VBE.
Debugging:
Syntax errors often detected by VBE immediately Debug menu and Debug toolbar (ViewToolbarscheck Debug) provide typical debugging functions.
17