You are on page 1of 13

CREATING MACROS TO AUTOMATE ROUTINE TASKS IN EXCEL AND WORD

Copyright 2005 By Paul S. Natanson, P. E.


First Draft December 15, 2004 - - - Latest Revision - November 18, 2005

For Presentation On April 14, 2005 At The Monthly Meeting Of The Morris (County, NJ) Micro Computer Club (M2C2) http://morrismicro.home.att.net/ And On June 2, 2005 At The Monthly Meeting Of The Computer Club of the ExxonMobil Annuitant (and Retirees) of the Morris (County, NJ) Area (EACMA) And On November 18, 2005 At The Monthly Windows Pains Meeting Of The Amateur Computer Group Of New Jersey (ACGNJ) http://www.acgnj.us/ To download your own copy of some of the handouts used in this presentation go to http://excelvbaprogramming.homestead.com/ and click on Free Courses. 1

Prerequisite For This Talk


There are 10 kinds of people in this world: those who understand binary, and those who dont.
Source: Seen on a T-shirt at the MIT (Massachusetts Institute Of Technology) college campus in Cambridge (Boston), Massachusetts.

Understanding the above joke is NOT a prerequisite for this presentation. The only pre-requisite for this talk is a basic working knowledge of Excel and Word (Versions 1997 or newer).
2

Agenda
Demonstrations. What is a macro? Security settings to allow macros to run. How to record (and save) a macro. How to run your saved macro. How to edit a saved macro. Special macros:
auto_open auto_close etc.

Help and additional resources.


Note: This PowerPoint handout is only a brief outline. For more detailed info, go to http://excelvbaprogramming.homestead.com/ and click on FREE COURSES where you can download a MS/Word document called MacrosPresentationHandoutVersion.doc.

MS/Word Toggle strikethrough (toggle between on and off) Control Shift S Color highlighted text (toggle through various colors) Control Shift R Change the spacing between paragraphs Several toolbar buttons Insert symbol (e.g., bullet, degree sign, legal, etc.) CSB, CSW, CSN, etc. Toggle superscripts and subscripts Cntrl Shft Y Preserve destination format during paste Control V Delete to end of line Cntrl Shft Delete Toggle the (in)visibility of hidden text (Control + H) Cntrl Shft I Keep lines together (or not) (toggle) Cntrl Shift K Return to last cursor position when opening a saved document Write a check MS/Excel Toggle strikethrough (for the selected cell) Control Shift S Toggle colors (cycle through red, blue and black) Control Shift R Wrap text and adjust row height to fit Calendar control Meeting attendance records and sign-in sheets Show page breaks Show page breaks automatically (AutoOpen) Not working? Record the time of day that a document is opened (AutoOpen) Launch a favorite file (from a customized Work dropdown menu) Animated X-Y graphs (e.g., plots of y=mx+b, Lissajous patterns, etc.) Filter and sort a financial data base or check book log Financial planning (lifespan of an annuity) Build a control chart Blue text: Simple / educational 4 Download stock prices Red text: Fun / interesting Solve a checkerboard puzzle

Demonstrations

Other Uses Of Macros


MS/Outlook (eMail)
Abort SEND if email SUBJECT line is empty. Customize your auto-signature with time, date, etc. Auto-reply to spam to remove from mailing list

Games
PSNs Blockade game

Malicious macros
Delete or move files, send emails, dial the phone, etc.

Anything you can do with your keyboard and mouse can be done with a macro, automatically.
5

What Is A Macro?
A macro is a set of computer instructions
(a mini-computer program), activated by an event, that tells the computer what to do. Macros are often written (or coded) in a computer programming language called Visual Basic For Applications (VBA), a specialized subset of Visual Basic (VB). A macro can be a simple single instruction (like coloring selected text red) or, it can be several thousand lines of complicated code (like building a stock price chart)

A macro can also involve decisions and branches


For example: If selected text is blue, Then color it red; Else, color it blue.

Security Settings To Allow Macros


Launch Excel, Word, or other Microsoft Office application. Click - - - Tools | Macro | Security. Set your security level to medium.
Affects all Microsoft Office applications (Word, Excel, etc.) Might need to restart the application before setting takes effect.

You can digitally sign your macros.


You, and other people who might use your macros can change their security setting to recognize your signature and allow macros signed by you to run without further prompting.

Caution:
The undo command does not always work with macros. (Before you run a macro be sure that you really want to.)
7

How To Record A Macro


(i.e., How To Record Keystrokes)

Launch Excel, Word, or any other MS/Office application. Click - - - Tools | Macro | Record New Macro. Select a place to store it:
Global: - - - Available to all documents, or Local: - - - - Restricted to the current document.

Option: May assign a shortcut key (Control + __ ). Click OK to begin recording keystrokes. IMPORTANT: When done recording, remember to click STOP RECORDING.
8

Running A Saved Macro


There are many ways to run / launch a saved macro. Here are just a few:
Select - - - Tools | Macro | Macros Use shortcut key(s) - - - (Control + __ or Control + Shift + __ ). Create a button (or other control) for it on your document. Program it to be auto-activated by an event or timer. Create a new button in one of your toolbars. Create a menu item for it on an existing, or new, drop-down menu. Add it to a right-click menu (a context-sensitive menu) triggered by a right-click event. Tie it to another event such as
Moving the cursor Selecting a different cell (in Excel) Opening a file Having no activity for a specified time period

Editing A Saved Macro


Launch Excel, Word, or any other Office application. Open the document containing the macro that you want to edit. Click - - - Tools | Macro | Macros Select the macro that you want to edit. Click Edit.
10

Special Macros
(Triggered By Special Events)

Word
AutoExec Runs automatically when you start Word AutoNew Runs automatically when you create a new document AutoOpen Runs automatically when you open an existing document
(E. g., An AutoOpen Macro can be used to set the cursor at last edit point.)

AutoClose Runs automatically when you close a document AutoExit Runs automatically when you quit Word

Excel
AutoLoad Runs automatically when an Excel workbook is opened) RunAutoMacros
RunAutoMacros xlAutoOpen RunAutoMacros xlAutoClose RunAutoMacros xlAutoActivate RunAutoMacros xlAutoDeactivate

These special macros are included in new versions of VBA for backward compatibility with older versions that use Auto_Open, Auto_Close, etc. For new Visual Basic code, you should use the Open, Close, Activate, and Deactivate events instead of these macros.
11

Conclusion
You can use macros to automate routine tasks in Excel, Word, or any other Microsoft Office application. The easiest way to create a macro is to record keystrokes and then edit (fine tune) the recording. The easiest ways to run a macro are to:
Assign it to a new button on your toolbar, or Assign it to a fast path on your keyboard, or Click - - - Tools | Macro | Macros

If you can do it with a keyboard and mouse, then you can automate it with a macro.
12

Help And Additional Resources


VBA (Visual Basic For Applications) Help screens (Press F1 from within the VBA code window). Right click on any word, term, command, etc. in your VBA code. Microsoft Office System Webcast: Tips and Tricks for Using Basic Word VBA Every Day: It's Much Easier than You Think! (Level 300)
http://msevents.microsoft.com/cui/r.aspx?r=5581587&c=en-US&t=4 or http://msevents.microsoft.com/cui/r.aspx?t=4&c=en-us&r=5581587 or http://msevents.microsoft.com/CUI/WebCastEventDetails.aspx?EventID=1032267697&Culture=en-US

Other Microsoft OFFICE webcasts (including more Tips And Tricks webcasts)
http://www.microsoft.com/seminar/events/series/connectedproductivity.mspx

Microsoft Electronic Newsletters


http://www.microsoft.com/seminar/events/series/connectedproductivity.mspx

PCWorld Magazine Article in the January 2002 issue entitled:


Office XP Tips: Macros 101 - - - - Take the tedium out of repetitive tasks with Office XP macros. http://www.pcworld.com/howto/article/0,aid,73644,00.asp

Microsoft VBA Newsgroups

Example: msnews.microsoft.com/microsoft.public.excel.programming
http://msdn.microsoft.com/

Microsoft Developers Network (MSDN) (for developers and more advanced users) Microsoft Online Training (webcasts, videos, other free online training)
http://office.microsoft.com/training Enter training and macro in the 2 search boxes at top of window and click go. Includes a course about writing your first Excel macro.

May 24, 2005 Issue Of PC Magazine Article on Pg 60 entitled Demystifying Excel Macros
http://www.pcmag.com/

Amateur Computer Group Of New Jersey (ACGNJ) VBA Special Interest Group (SIG)
Meets in Scotch Plains, NJ - http://www.acgnj.org/ or http://s90950543.onlinehome.us/acgnj/groups/vbasig.html

My (Paul S. Natansons) website: http://excelvbaprogramming.homestead.com/index.html

13

You might also like