Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual Basic for Applications Macro Programming For Microsoft Office.

Similar presentations


Presentation on theme: "Visual Basic for Applications Macro Programming For Microsoft Office."— Presentation transcript:

1 Visual Basic for Applications Macro Programming For Microsoft Office

2 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

3 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

4 Recording Macros Tools -> Macro ->Record New Stop Toolbar Appears Excel records every stroke until you click the “Stop” button Replay with: Tools ->Macro->Macros

5 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

6 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:

7 What is VBA Powerful Programming Language Object Oriented Objects: Cells, Worksheets, Menus Range(“C3”) Objects You control the Properties of an Object Range(“C3”).Font.NameProperties Assignment Statements define property state Range(“C3”).Font.Name = “Arial” Assignment Statements

8 Objects on a Spreadsheet Workbook Column Row Cell Range Worksheet

9 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

10 Object Hierarchy Application 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

11 Cell Properties Name - [A4] Font - Comic Sans Alignment – Right Text color - Red Value - 13 Formula: =SUM(A1:A3)

12 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

13 VBA Editor Tools -> Macro -> Visual Basic Editor Shortcut: Alt+F11

14 VBA Editor – Project Window Worksheets Workbook Modules

15 VBA Editor – Code Window Color Coding Key Words Comments Errors Objects Properties Methods

16 Subroutines Begin With Sub MyName() End with End Sub Location Modules Worksheets Workbook UserForm

17 Read your code Move cursor to cell C7 Write the word “Hello” in the active cell Put cursor in cell C6 Change font color to blue Comments: VBA ignores these lines Stop the macro Macro Name

18 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

19 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

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

21 InputBox Function Asks user to supply information Returns a text string Inputbox(“Enter your name: ”,”Name”) Prompt Title Cannot stand alone Range(“B3”) = Inputbox(“Enter your name”,”Name”)

22 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

23 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

24 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

25 Events – Running Macros Selecting Menu Items Tools->Macro->Macros Shortcut Keys Ctrl-Z Buttons


Download ppt "Visual Basic for Applications Macro Programming For Microsoft Office."

Similar presentations


Ads by Google