An object-oriented programming language ◦ Instructions for the manipulation of objects ◦ A...

13
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) Visual Basic for Applications (VBA)

Transcript of An object-oriented programming language ◦ Instructions for the manipulation of objects ◦ A...

Page 1: An object-oriented programming language ◦ Instructions for the manipulation of objects ◦ A structured way to provide instructions to Excel  Excel has.

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)

Visual Basic for Applications (VBA)

Page 2: An object-oriented programming language ◦ Instructions for the manipulation of objects ◦ A structured way to provide instructions to Excel  Excel has.
Page 3: An object-oriented programming language ◦ Instructions for the manipulation of objects ◦ A structured way to provide instructions to Excel  Excel has.

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

Logic of Language

Page 4: An object-oriented programming language ◦ Instructions for the manipulation of objects ◦ A structured way to provide instructions to Excel  Excel has.

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

Example of an Instruction

Page 5: An object-oriented programming language ◦ Instructions for the manipulation of objects ◦ A structured way to provide instructions to Excel  Excel has.

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

Control of VBA

Page 6: An object-oriented programming language ◦ Instructions for the manipulation of objects ◦ A structured way to provide instructions to Excel  Excel has.

Developer Tab (Excel 2010)

Page 7: An object-oriented programming language ◦ Instructions for the manipulation of objects ◦ A structured way to provide instructions to Excel  Excel has.

Sub bluecell()'' bluecell Macro' With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 16777062 .TintAndShade = 0 .PatternTintAndShade = 0 End WithEnd Sub

BlueCell Macro

Page 8: An object-oriented programming language ◦ Instructions for the manipulation of objects ◦ A structured way to provide instructions to Excel  Excel has.

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.

With…..End With construct

Page 9: An object-oriented programming language ◦ Instructions for the manipulation of objects ◦ A structured way to provide instructions to Excel  Excel has.

Run Macro option

Shortcut Keys: [Ctrl][Shift] letter

Command Buttons

Custom Toolbar

Running Macros

Page 10: An object-oriented programming language ◦ Instructions for the manipulation of objects ◦ A structured way to provide instructions to Excel  Excel has.

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.

Variables in VBA

Page 11: An object-oriented programming language ◦ Instructions for the manipulation of objects ◦ A structured way to provide instructions to Excel  Excel has.

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

Declaring Variables

Page 12: An object-oriented programming language ◦ Instructions for the manipulation of objects ◦ A structured way to provide instructions to Excel  Excel has.

Function name(parameter1, parameter 2,…) …Code…. name= result End Function

Example: Function betapert(prob,min,mostlikely,max) …Code…. betapert= Application.BetaInv(…..) End Function

User-defined Functions

Page 13: An object-oriented programming language ◦ Instructions for the manipulation of objects ◦ A structured way to provide instructions to Excel  Excel has.

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.

Debugging VBA Code