Sei sulla pagina 1di 3

Quick Reference Card - Get in the loop with Excel macros

Macro basics
Action Get into the Visual Basic Editor Insert a module Run a macro Three types of loop macros To make VBA case insensitive To show columns as numbers in Excel How to On the Tools menu, point to Macro, and then click Visual Basic Editor. Select Module on the Visual Basic Editor's Insert menu (not the spreadsheet's menu). Click the Sub MyMacroName() line and press F5 to run your macro. The DoLoop, the For EachNext Loop, and the Nested Loop. Add "Option Compare Text" to the top of your module, before any macros. On the Tools menu, click Options, and then select the General tab. Check the "R1C1 reference style" box.

Tips for working with macros


A great way to create your own code example is to record a macro for the action first. On the Tools menu, point to Macro and select Record New Macro. Once you've recorded the macro, take a look at it in the Visual Basic Editor to see how its code works. On the Tools menu, point to Macro and then select Macros to find the one you recorded. Select it and click Edit to see the code.

If you want to copy a macro and then tweak that copy, make sure to rename the second macro or else Visual Basic will be confused about which macro it should run. For example, if you have Sub MyMacro(), copy the entire macro and paste it below the "End Sub" line. Then change the name on the copy to MyMacro1 so it is, for example, Sub MyMacroCopy().

If you want to run any existing macro in the main Microsoft Excel window, use the method described above to open the Macro dialog box (or use the keyboard shortcut Alt+F8), find the macro, and then click Run.

You can attach toolbar buttons to macros. On the Tools menu, select Customize. In the Customize dialog box, select Macros under Categories on the Commands tab. Under Commands, click Custom Button and drag it to an existing toolbar in the main Excel window. Next, right-click the new button and select Assign Macro to see a list of available macros. You can change the appearance of the new button by selecting Change Button Image, also found on the right-click menu.

Keyboard shortcuts
These shortcut keys will save you time when you work with macros:

Alt+F11: Switches to and from the Visual Basic Editor. Alt+F8: Displays a list of macros available in the workbook. F5: Runs a macro in the Visual Basic Editor.

A few notes about security


As you are probably aware, macros from unknown sources can cause harm to your computer. By default, Microsoft Office applications ship with a macro security level of "High," which is the best setting for everyday use of Office because it helps

prevent the spread of macro viruses and disables inadvertent code from running on your computer.

There is a trade-off between ease of use and increased security. In this course, your macro security level will not matter because new macros are not subject to the security check. To confirm your security level, choose Macro and then Security from the Tools menu.

If you choose to run macros, you need to take steps to help yourself be more secure. The best way to allow macros to run is to sign your workbook. There is a special program called "Digital Certificate for VBA Projects," which is available on the Microsoft Office Tools menu under Microsoft Office in the Windows Start menu. This program allows you to create a self-signed digital certificate that can be used to sign your workbook. Signing a workbook that contains code will allow you to trust your selfsigned certificate when you open the workbook and maintain a macro security level of "High."

Creating and using a digital certificate for a VBA project


Step To run the "Digital Certificate for VBA Projects" program Type your name and press OK twice In the Visual Basic Editor, select Digital Signature... from the Tools menu Click the Choose button Select the certificate you created, and press OK twice Note If you modify your Visual Basic Project, for example by changing the macros or adding a new module, you will need to resign your project. Related Information From the Windows Start menu, select Microsoft Office, then Microsoft Office Tools. In the Your certificate's name section, type your name or any other value that you wish to call your certificate.

Resources and code examples


Non-Microsoft Web sites run by Microsoft Excel Most Valuable Professionals (MVPs) and others:

VBA Code Examples, by Nick Hodge, Excel MVP VBA for Charts, by Jon Peltier, Excel MVP The Spreadsheet Page, from J-Walk & Associates, Inc. Daily Dose of Excel, a blog from Dick Kusleika, Excel MVP Excel Tips, from Ron de Bruin, Excel MVP Free Excel downloads, from Ozgrid Business Applications The blog of Andrew Engwirda, "incurable Excel addict"

Microsoft Knowledge Base articles about working with VBA code in Excel:

KB 305326: List of programming resources for Visual Basic for Applications KB 213631: Sample Macro to Insert/Delete Rows or Columns on Multiple Sheets KB 213550: Sample Macros for Customizing Menus and Submenus

Books on Excel and VBA programming:

Walkenbach, John. Excel VBA Programming For Dummies. New York, NY: Wiley, 2004. Frye, Curtis. Microsoft Office Excel 2003 Programming Inside Out. Redmond, WA: Microsoft Press, 2004. Walkenbach, John. Excel 2003 Power Programming with VBA. Indianapolis, IN: Wiley, 2004. Walkenbach, John. Excel 2003 Bible. Indianapolis, IN: Wiley, 2003. Roman, Steven. Writing Excel Macros with VBA. 2nd ed.Cambridge, MA: O'Reilly, 2002. Aitken, Peter. Excel Programming Weekend Crash Course. New York, NY: Wiley, 2003. Getz, Ken, and Gilbert, Mike. VBA Developer's Handbook. 2nd ed. Alameda, CA: Sybex, 2000.

Potrebbero piacerti anche