Download presentation
Presentation is loading. Please wait.
Published byAllan Henderson Modified over 9 years ago
1
An object-oriented programming language ◦ Instructions for the manipulation of objects ◦ A structured way to provide instructions to Excel Excel has an Object Library that defines its set of objects (e.g. workbooks, charts, ranges of cells)
3
First part of a command identifies the object Second part of command: ◦ Sets a property of the object (e.g. color or font) ◦ Takes an action on the object (e.g.copy or move) ◦ Sets an attribute to the object (e.g. assigns a value to it using := )
4
Application.Workbooks(“Book1.xls”). Worksheets(“Sheet1”).Range(“A1”).Font. Name=“Arial” ◦ Application.Workbooks(“Book1.xls”). Worksheets(“Sheet1”).Range(“A1”) is the object ◦ Font.Name=“Arial” is the property being set
5
A Procedure is a set of instructions that operate on a set of objects ◦ A Function is a procedure that returns a value as a result ◦ A Subroutine is a procedure that can be run or used by another macro
7
Sub bluecell() ' ' bluecell Macro ' With Selection.Interior.Pattern = xlSolid.PatternColorIndex = xlAutomatic.Color = 16777062.TintAndShade = 0.PatternTintAndShade = 0 End With End Sub
8
Used to overcome long lines of repetitive code that can arise when using full references to objects The construct allows for repeated referral to the same object and thereby reduces the amount of code Selection.Interior is the object and the next five lines before the End With represent the different properties being set for this object.
9
Run Macro option Shortcut Keys: [Ctrl][Shift] letter Command Buttons Custom Toolbar
10
Variables contain values ◦ X= 3000 ◦ UnitCost = 1.20 ◦ Output1 = “npv” Each variable has a location in memory where its value is stored If a variable is on left side of the equal sign, then the variable’s new value in memory is changed to whatever is on right side If a variable is on right side of the equal sign, then the variable’s value will not change but its value will be assigned to another variable or object.
11
Declare all variables at beginning of each procedure with the keyword Dim ◦ Catches spelling problems later on and helps debug problems ◦ Allows you to specify type of variable: String (for names like “npv”) Integer Single (for numbers with decimals) Currency Boolean (for True/False) ◦ Uses memory better and allows variable to be used in its appropriate context
12
Function name(parameter1, parameter 2,…) …Code…. name= result End Function Example: Function betapert(prob,min,mostlikely,max) …Code…. betapert= Application.BetaInv(…..) End Function
13
When VBA encounters a problem, it will highlight line in yellow. Use [f8] key to step through code Use the Reset icon button (square) at top of VBA to exit step-through mode and trouble- shoot code or Excel/VBA settings. Cannot retry macro until yellow highlight is gone.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.