Visual Basic for Applications Macro Programming For Microsoft Office.

25
Visual Basic for Applications Macro Programming For Microsoft Office

Transcript of Visual Basic for Applications Macro Programming For Microsoft Office.

Page 1: Visual Basic for Applications Macro Programming For Microsoft Office.

Visual Basic for Applications

Macro ProgrammingFor Microsoft Office

Page 2: Visual Basic for Applications Macro Programming For 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

Page 3: Visual Basic for Applications Macro Programming For Microsoft Office.

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

Page 4: Visual Basic for Applications Macro Programming For Microsoft Office.

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

you click the “Stop” button Replay with:

Tools ->Macro->Macros

Page 5: Visual Basic for Applications Macro Programming For Microsoft Office.

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

Page 6: Visual Basic for Applications Macro Programming For Microsoft Office.

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:

Page 7: Visual Basic for Applications Macro Programming For Microsoft Office.

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”

Page 8: Visual Basic for Applications Macro Programming For Microsoft Office.

Objects on a Spreadsheet

Workbook

Column

Row

Cell

Range

Worksheet

Page 9: Visual Basic for Applications Macro Programming For Microsoft Office.

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

Page 10: Visual Basic for Applications Macro Programming For Microsoft Office.

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

Page 11: Visual Basic for Applications Macro Programming For Microsoft Office.

Cell Properties

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

=SUM(A1:A3)

Page 12: Visual Basic for Applications Macro Programming For Microsoft Office.

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

Page 13: Visual Basic for Applications Macro Programming For Microsoft Office.

VBA Editor

Tools -> Macro -> Visual Basic Editor

Shortcut: Alt+F11

Page 14: Visual Basic for Applications Macro Programming For Microsoft Office.

VBA Editor – Project Window Worksheets Workbook Modules

Page 15: Visual Basic for Applications Macro Programming For Microsoft Office.

VBA Editor – Code Window Color

Coding Key Words Comments Errors

Objects Properties Methods

Page 16: Visual Basic for Applications Macro Programming For Microsoft Office.

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

Modules Worksheets Workbook UserForm

Page 17: Visual Basic for Applications Macro Programming For Microsoft Office.

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

Page 18: Visual Basic for Applications Macro Programming For Microsoft Office.

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

Page 19: Visual Basic for Applications Macro Programming For Microsoft Office.

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

Page 20: Visual Basic for Applications Macro Programming For Microsoft Office.

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)

Page 21: Visual Basic for Applications Macro Programming For Microsoft Office.

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”)

Page 22: Visual Basic for Applications Macro Programming For Microsoft Office.

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

Page 23: Visual Basic for Applications Macro Programming For Microsoft Office.

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

Page 24: Visual Basic for Applications Macro Programming For Microsoft Office.

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

Page 25: Visual Basic for Applications Macro Programming For Microsoft Office.

Events – Running Macros Selecting Menu Items

Tools->Macro->Macros Shortcut Keys

Ctrl-Z Buttons