Sunteți pe pagina 1din 9

VBA and Excel Solver for a Simple Decision Support System

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.

VBA comes with MS Office, which is available in most places.

Wyndor Glass Problem

Download excel model file of Wyndor Glass problem from ICON. Save the file as an Excel Macro-Enabled Workbook (.xlsm) file.

Developer Tab and VBE

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

Add a Dialog Box

In VBE, select the project (WyndorGlass.xlsm) in the project explorer window. InsertUserForm Select the new form. In the Properties window, set

Project explorer

Name = frmInputs Caption = Input Data Properties window

Add Controls to frmInputs


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.

Add Codes for frmInputs


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():

Select control here

Select event here

Codes for btnOk_Click (without error checking)

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.

Explanation of VBA Codes


Sub/End Sub defines a subroutine/procedure. With construction:


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.

Add Error Checking

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

For Loops in VBA:


For counter = first To last statements Next

11

Codes for UserForm_Initialize


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

Add Module and Main Codes


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

Explanation of Codes (Contd)

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 function: displays a message in a dialog box


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

Help and Debug

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

S-ar putea să vă placă și