Creating Macros To Automate Routine Tasks In: Excel and Word
Creating Macros To Automate Routine Tasks In: Excel and Word
Creating Macros To Automate Routine Tasks In: Excel and Word
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 don’t.
Source: Seen on a T-shirt at the MIT (Massachusetts
Institute Of Technology) college campus in
Cambridge (Boston), Massachusetts.
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. 3
• MS/Word Demonstrations
– 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
– Download stock prices Blue text: Simple / educational
4
– Solve a “checkerboard” puzzle Red text: Fun / interesting
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
– PSN’s “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.
6
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)
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
• Microsoft Developers Network (MSDN) – (for developers and more advanced users)
– http://msdn.microsoft.com/
• 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
13
• My (Paul S. Natanson’s) website: http://excelvbaprogramming.homestead.com/index.html