microsoft excel 2007 © wiley publishing. 2007. all rights reserved. the l line the express line to...

15
Microsoft Microsoft Excel 2007 Excel 2007 © Wiley Publishing. 2007. All Rights Reserve The L Line The L Line he Express Line to Learning he Express Line to Learning L Line

Upload: terence-kennedy

Post on 27-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Microsoft Excel 2007 © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line

MicrosoftMicrosoftExcel 2007Excel 2007

© Wiley Publishing. 2007. All Rights Reserved.

The L LineThe L LineThe Express Line to LearningThe Express Line to Learning

L Line

Page 2: Microsoft Excel 2007 © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line

Programming Excel: An Introduction to VBA and Macros

15

• Running your first macro• Creating your own macro• Activating and using the Developer tab• Debugging macros in the Visual Basic Editor• Adapting a macro• Adding a macro to the QAT• Looking at other uses for macros• Understanding trusted locations and trusted sources

Stations Along the Way

Page 3: Microsoft Excel 2007 © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line

Why Use Macros?

Macros enable you to automate a process you perform in Excel:• A series of steps you perform regularly• A complicated process you need to be

able to repeat exactly each timeThey make it easier to do your workRecord your keystrokes and then use

VBA to fine-tune macros (or create them from scratch)

Page 4: Microsoft Excel 2007 © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line

Sample Macro Code

This macro code performs a progression• First line defines name; lines that begin with

quotation marks are comments; the macro follows

Sub progression1()'' This macro takes two numbers from consecutive cells in' column C and finds the next 10 numbers in the progressive' series' For I = 3 To 13 nextvalue = "c" & I Range(nextvalue).Select ActiveCell.FormulaR1C1 = "=R[-2]C+R[-1]C" Next IEnd Sub

Page 5: Microsoft Excel 2007 © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line

Creating a Macro

Easiest method is to record the macro; Excel watches what you do and remembers the steps

Access the Record Macro command from the View tab (Macros button)

Name the macro, assign shortcut key, and specify location and description

Record the macro and then stop recording when macro steps complete

Page 6: Microsoft Excel 2007 © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line

Tips for Creating Macros

Practice what you want the macro to accomplish; jot down steps beforehand

Give macros a descriptive nameUse an unassigned shortcut keyStep through the macro to debug itEdit macro code or re-record macroSave macro file in .xlsm format

Page 7: Microsoft Excel 2007 © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line

Using the Developer Tab

More options for working with macrosDeveloper tab doesn’t display by

default; you must activate itSelect “Show Developer Tab in the

Ribbon” in the Excel Options window (Popular options)

Developer tab displays in the Ribbon with four groups: Code, Controls, XML, and Modify

Page 8: Microsoft Excel 2007 © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line

Viewing Code in the Visual Basic Window

Open file containing macros and choose Visual Basic on Developer tab

Visual Basic window has four areas:• Command menus across the top• Top-left window shows project elements• Bottom-left window shows properties• Large window on right shows VBA code

Double-click a module to see code

Page 9: Microsoft Excel 2007 © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line

Debugging Macros

Most code includes errors to debug:• Syntax – typos and command errors• Logic – Excel doesn’t understand coding• Scope – code works, but not optimally

Run macro with error; Visual Basic Editor opens and highlights error

Note type of error in message boxUse Debug button and Help infoFix coding and rerun until error-free

Page 10: Microsoft Excel 2007 © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line

Adapting a Macro

Control what a macro does by:• Asking for user input to determine path• Basing macro path on a condition• Placing processing in a repeating loop

that controls the code

Add helpful comments for users:• Describe in detail what the macro does• Explain limits, conditions, variable names

Page 11: Microsoft Excel 2007 © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line

Adding a Macro to the QAT

Add a button to the QAT to quickly access your macro

Click Customize in the Excel Options window and choose Macros category

Specify current file or all filesSelect macro name and add to QATClick Modify to add custom iconClick button in QAT to run the macro

Page 12: Microsoft Excel 2007 © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line

Other Uses for Macros

Creating complex chartsPerforming complicated computationsApplying detailed worksheet formattingSimplifying data entryOpening and cleaning text and CSV

filesWriting VBA applications that run on

top of Excel

Page 13: Microsoft Excel 2007 © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line

Trusted Locations and Trusted Sources

Trusted locations contain code that is trusted from any source• Includes default template locations, user

startup folders, Excel add-ins location• Use Trust Center to add trusted locations

Trusted sources include trusted publishers and companies who develop coding and use certificates to verify their identity

Page 14: Microsoft Excel 2007 © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line

Discussion Questions

Why do you need to enable macros?Which format should you use when

saving Excel files with macros in them?Why would you use the Developer tab?Why would you want a macro to

contain an If...Then statement?Why would you want a macro to

contain a loop?

Page 15: Microsoft Excel 2007 © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line

Discussion Questions (cont.)

Where do you debug macros?What is the difference between a

syntax error and a scope error?Why should you trap errors?How do you add a macro to the QAT

(Quick Access Toolbar)?What is a trusted source?