VBA Programming in Excel For Decision Support Systems
VBA Programming in Excel For Decision Support Systems
VBA Programming in Excel For Decision Support Systems
Modeling
Professor Munson
Topic 1
VBA Programming in Excel for
Decision Support Systems
Definitions
Objects correspond to nouns, properties
correspond to adjectives, methods correspond to
verbs, and arguments correspond to adverbs.
Help
Object Browser (<F2>)
Lists all objects, properties, and methods
Locals Window Quick Watch
Displays values Selects a variable to
of all variables display value in the
Watch Window
Immediate Window
Lets you enter and test a
single line of code
outside of the SUB
Uncheck:
→Tools→Options→Editor→Auto Syntax Check
Typing
Indent for readability (use the <Tab> key or
→Edit→Indent). To remove an indentation, use
<Shift><Tab> or →Edit→Outdent)
Add blank spaces for readability
To wrap a line, insert a blank then _ then a blank;
otherwise, each new line is considered to be a
separate command
Insert comments with a single quotation mark
Red color: you typed something wrong
Blue color: for all keywords (Sub, End, For)
Green color: for all comments
SAVE OFTEN!
Variables
Variables contain values or strings of text and
can be modified during the course of a program.
Other options:
vbOKCancel, vbAbortRetryIgnore, vbRetryCancel,
vbYesNoCancel, vbOKOnly
Activation Button:
Insert a Button Form Control (under
Developer →Controls→Insert→Form Controls)
and select the SUB that you want the button
to activate (just like creating one for a
macro).
Exercise 3
The file Input Output 3_1.xlsx (on the course website) is a
template for calculating the total order cost for ordering a
product with quantity discounts. The table (range-named
LTable) in the range A4:C8 contains unit costs for various order
quantity intervals. The range B11:B13 contains a typical order
cost calculation, where the input is the order quantity in Cell
B11 and the ultimate output is the total cost in Cell B13. Take a
look at this file to see how a VLOOKUP function is used to
calculate the appropriate unit cost in Cell B12.
Colors
vbBlack, vbBlue, vbCyan, vbGreen,
vbMagenta, vbRed , vbWhite, and vbYellow
Horizontal Alignment
xlRight, xlLeft, and xlCenter
WorksheetFunction.
As soon as you type the last period, a list of
most Excel functions appears from which you
can select.
Examples
A B C D E
1 6 4 2 8 10
2 74.8
WorksheetFunction.MAX(Range(“A1:E1”))
WorksheetFunction.SUM(Range(“A1:E1”))
WorksheetFunction.AVERAGE(Range(“A1:E5”))
WorksheetFunction.FLOOR(Range(“A2”),1)
WorksheetFunction.RANDBETWEEN(1,6)
Examples
1. Function for the maximum of two numbers
End Function
Code for the Exercises
Exercise 1
Public Sub RevenueCalc()
End Sub
Exercise 2
Public Sub RevenueCalc2()
End Sub
Exercise 3
Public Sub Create()
Dim Q1 As Long
Dim Q2 As Long
Dim Q3 As Long
Q1 = InputBox("Enter the first order quantity.")
Q2 = InputBox("Enter the second order quantity.")
Q3 = InputBox("Enter the third order quantity.")
Range("B11").Value = Q1
Range("D12").Value = Q1
Range("E12").Value = Range("B13").Value
Range("B11").Value = Q2
Range("D13").Value = Q2
Range("E13").Value = Range("B13").Value
Range("B11").Value = Q3
Range("D14").Value = Q3
Range("E14").Value = Range("B13").Value
End Sub