Visual Basic for Applications Macro Programming For Microsoft Office.

Post on 01-Jan-2016

231 views 1 download

transcript

Visual Basic for Applications

Macro ProgrammingFor Microsoft Office

Macros Macros are programs that make

changes to a spreadsheet automatically Change a font size or color Move the cursor to another cell Change the contents of a cell Almost anything you can do from the

menus can be automated Simple process – record a macro

Why Create Macros? Automate tasks

Ones you do repetitively Complex tasks with

possibilities for errors Create custom functions Develop complete applications

Create macros that perform specific tasks Customize the menus and toolbars Create custom “front-ends” for users

Recording Macros Tools -> Macro ->Record New Stop Toolbar Appears Excel records every stroke until

you click the “Stop” button Replay with:

Tools ->Macro->Macros

Record a Macro Turn on recorder

Name macro “MyFirstMacro” Move to cell C6 Change text color to Blue Enter “Hello”

Click “Stop” button Reset cell C6 Run macro

It Works! Where is it? Macro recorder writes VBA code Stored with the spreadsheet When you save the workbook,

you save any code as well. Looks like this:

What is VBA Powerful Programming Language Object Oriented

Objects: Cells, Worksheets, MenusRange(“C3”)

You control the Properties of an ObjectRange(“C3”).Font.Name

Assignment Statements define property stateRange(“C3”).Font.Name = “Arial”

Objects on a Spreadsheet

Workbook

Column

Row

Cell

Range

Worksheet

VBA Objects Usually referred to by a name

Cell “B6” is named Range(“B6”) Column(“B:B”) refers to the entire Column

Nicknames for current cells, sheets ActiveCell ActiveSheet

Objects of same type form a collection Worksheets(…) is the collection Worksheets(“Sheet1”) refers to one object

Object HierarchyApplication CommandBar Name Window Workbooks Others...

Worksheet AutoFilter Name PageSetup PivotTable Range Others...

Workbook Charts Name Style Window Worksheets Others...

A Workbook may contain many sheets Workbooks(“Book1.xls”).Worksheets(“Sheet1”) Active objects used by default

Cell Properties

Name - [A4] Font - Comic Sans Alignment – Right Text color - Red Value - 13 Formula:

=SUM(A1:A3)

Assignment Statements Assignment statements change an object This statement puts the number 15

into cell A3: Range(“A3”).value = 15

This statement determines the value in cell A3, and puts it in cell C4: Range(“C4”).value = Range(“A3”).value

This one adds the value in A3 to what’s in C4: Range(“C4”).value = Range(“C4”).value + _

Range(“A3”).value

VBA Editor

Tools -> Macro -> Visual Basic Editor

Shortcut: Alt+F11

VBA Editor – Project Window Worksheets Workbook Modules

VBA Editor – Code Window Color

Coding Key Words Comments Errors

Objects Properties Methods

Subroutines Begin With Sub MyName() End with End Sub Location

Modules Worksheets Workbook UserForm

Read your code

Move cursor to cell C7

Write the word “Hello” in the active cell

Put cursor in cell C6Change font color to blue

Comments: VBA ignores these lines

Stop the macro

Macro Name

Relative Addresses Defines one cell relative to another Offset(Row, Column)

Rows: positive to right Columns: positive means move down Range(“A3”).Offset(2,4) refers to cell E5 Activecell.Offset(1,0).Select moves cursor

down one cell. Recording Macros

Relative Cell Address Switch

Variables Short-term storage for information

Held in RAM, not stored to disk Disappears when program stops running

Needs a name Must be unique

Can’t be the same as objects, other variables Can’t be a key word to VBA I often use vName

Dim vName

Functions Usually accept an argument Returns a statement or value

Abs(vNum)

Argument: number stored in vNum

Function: Abs( ) Determines absolute value of argument

Returns that value

Can’t stand alone – must pass Use assignment statement

vPosNum = Abs(vNum)

InputBox Function Asks user to supply information Returns a text string

Inputbox(“Enter your name: ”,”Name”)

Prompt

Title

Cannot stand aloneRange(“B3”) = Inputbox(“Enter your

name”,”Name”)

MsgBox Function Used to display a message Simple form: MsgBox message

MsgBox “Hi, Bob!” Can ask the user for Yes/No

feedback Use Help to explore more complex

form

Sample Macro

Start with macro name

Define a variable to store text string.

Use InputBox to get name, store in variable.

Use MsgBox to display “Hi, “ plus the stored name.

Finish with End Sub

What is VBA

Event Driven Mouse click, Change in an object, etc. VBA responds only when an event

occurs You decide which events start VBA

Powerful Programming Language Object Oriented

Events – Running Macros Selecting Menu Items

Tools->Macro->Macros Shortcut Keys

Ctrl-Z Buttons