microsoft excel 2007 © wiley publishing. 2007. all rights reserved. the l line the express line to...
TRANSCRIPT
MicrosoftMicrosoftExcel 2007Excel 2007
© Wiley Publishing. 2007. All Rights Reserved.
The L LineThe L LineThe Express Line to LearningThe 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
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)
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
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
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
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
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
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
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
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
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
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
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?
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?