Table Of Contentspine=.8160”
Computers/Spreadsheets
Making Everything Easier!™ 2nd Edition
Accelerate into the Excel
fast lane and zip through 2nd Edition
spreadsheet customizations Open the book and find: Ex Mic EMicrxosofct® e l ® VBA
r
o
Are you ready to make Excel jump through hoops? Do (cid:129) How to work in the Visual Basic® csof
your spreadsheets work exactly the way you want? With Editor et ®
l ng
this book, you can say “yes”! Inside, you’ll find step-by-step ® mi
instructions on how to significantly enhance the power of (cid:129) Texiptse rfmori nerartoior-nh andling and bug VB Progra m
Excel using the VBA language. You’ll soon be building your
(cid:129) W ays to build automatic A
own Excel applications so you can get the most out of
procedures and events
your data. P
(cid:129) How to use the Excel macro r
(cid:129) Dive into VBA — see how VBA is implemented in Excel, apply recorder o
advanced techniques, and use VBA with other Office applications g
(cid:129) UserForm techniques and tricks
r
(cid:129) Get into it — explore programming concepts from range objects
a
and worksheet functions to events and error-handling (cid:129) Instructions for designing VBA
macros m
(cid:129) Communication is key — learn how to build dialog boxes that
look like they came straight from Microsoft’s software lab (cid:129) Steps for accessing your macros m
through the user interface
(cid:129) Safety first — use Excel macros to make your spreadsheets more i
n
secure and lock out viruses (cid:129) Secrets for creating Excel add-ins
g
(cid:129) Make it your own — develop custom worksheet functions, create
add-ins, design user-oriented applications, and boost your
career options
Learn to:
(cid:129) Use the essential tools and operations
Go to Dummies.com
® for VBA
Visit the companion Web site at http://www.dummies.com/
for videos, step-by-step examples,
go/excelvbaprogrammingfd2e, where you can download the how-to articles, or to shop! (cid:129) Work with range objects and control
program flow
sample files featured in the book and view bonus chapters
(cid:129) Handle errors and eliminate bugs
in your code
(cid:129) Develop custom user interfaces for your
$29.99 US / $35.99 CN / £21.99 UK
applications, including dialog boxes
John Walkenbach is principal of J-Walk and Associates, Inc., a leading ISBN 978-0-470-50369-0
authority on spreadsheet software and creator of the award-winning
Power Utility Pak. Walkenbach has written more than 50 books and 300
articles for publications including PC World, InfoWorld, and Windows.
John Walkenbach
Author of Excel 2010 Bible
Walkenbach
spine=.8160”
Get More and Do More at Dummies.com
®
Start with FREE Cheat Sheets
Cheat Sheets include
Apps
(cid:129) Checklists Mobile
(cid:129) Charts
(cid:129) Common Instructions
(cid:129) And Other Good Stuff!
To access the Cheat Sheet created specifically for this book, go to
www.dummies.com/cheatsheet/excelvbaprogramming
Get Smart at Dummies.com
Dummies.com makes your life easier with 1,000s
of answers on everything from removing wallpaper
to using the latest version of Windows.
Check out our
There’s a Dummies App for This and That
(cid:129) Videos
(cid:129) Illustrated Articles
(cid:129) Step-by-Step Instructions
With more than 200 million books in print and over 1,600 unique
Plus, each month you can win valuable prizes by entering titles, Dummies is a global leader in how-to information. Now
our Dummies.com sweepstakes. * you can get the same great Dummies information in an App. With
topics such as Wine, Spanish, Digital Photography, Certification,
Want a weekly dose of Dummies? Sign up for Newsletters on
and more, you’ll have instant access to the topics you need to
(cid:129) Digital Photography
know in a format you can trust.
(cid:129) Microsoft Windows & Office
(cid:129) Personal Finance & Investing
To get information on all our Dummies apps, visit the following:
(cid:129) Health & Wellness
(cid:129) Computing, iPods & Cell Phones www.Dummies.com/go/mobile from your computer.
(cid:129) eBay
www.Dummies.com/go/iphone/apps from your phone.
(cid:129) Internet
(cid:129) Food, Home & Garden
Find out “HOW” at Dummies.com
*Sweepstakes not currently available in all countries; visit Dummies.com for official rules.
Excel® VBA
Programming
FOR
DUMmIES
‰
2
ND EDITION
by John Walkenbach
0011__550033669900--ffffiirrss..iinndddd ii 44//1122//1100 1111::2200 PPMM
Excel® VBA Programming For Dummies®, 2nd Edition
Published by
Wiley Publishing, Inc.
111 River Street
Hoboken, NJ 07030-5774
www.wiley.com
Copyright © 2010 by Wiley Publishing, Inc., Indianapolis, Indiana
Published by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or
by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permit-
ted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written
permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the
Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600.
Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley
& Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://
www.wiley.com/go/permissions.
Trademarks: Wiley, the Wiley Publishing logo, For Dummies, the Dummies Man logo, A Reference for the
Rest of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, Making Everything
Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/
or its affi liates in the United States and other countries, and may not be used without written permission.
Excel is a registered trademark of Microsoft Corporation in the United States and/or other countries. All
other trademarks are the property of their respective owners. Wiley Publishing, Inc. is not associated with
any product or vendor mentioned in this book.
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO
REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF
THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITH-
OUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE
CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES
CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE
UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR
OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF
A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE
AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZA-
TION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF
FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE
INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY
MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK
MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT
IS READ. FULFILLMENT OF EACH COUPON OFFER IS THE SOLE RESPONSIBILITY OF THE OFFEROR.
For general information on our other products and services, please contact our Customer Care
Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002.
For technical support, please visit www.wiley.com/techsupport.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may
not be available in electronic books.
Library of Congress Control Number: 2010925699
ISBN: 978-0-470-50369-0
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
0011__550033669900--ffffiirrss..iinndddd iiii 44//1122//1100 1111::2200 PPMM
About the Author
John Walkenbach is the bestselling author of more than 50 spreadsheet
books and lives in southern Arizona. When he’s not using Excel, he’s prob-
ably playing clawhammer banjo.
0011__550033669900--ffffiirrss..iinndddd iiiiii 44//1122//1100 1111::2200 PPMM
0011__550033669900--ffffiirrss..iinndddd iivv 44//1122//1100 1111::2200 PPMM
Dedication
This book is dedicated to Toad and Mrs. Toad because I’m running out of
people for book dedications.
Author’s Acknowledgments
I’m grateful to all the talented people at Wiley Publishing for giving me the
opportunity to write Excel books. Special thanks to Colleen Totz Diamond
(project editor) and Jan Karel Pieterse (technical editor). They certainly
made my job easier.
0011__550033669900--ffffiirrss..iinndddd vv 44//1122//1100 1111::2200 PPMM
Publisher’s Acknowledgments
We’re proud of this book; please send us your comments at http://dummies.custhelp.com.
For other comments, please contact our Customer Care Department within the U.S. at 877-762-2974,
outside the U.S. at 317-572-3993, or fax 317-572-4002.
Some of the people who helped bring this book to market include the following:
Acquisitions, Editorial, and Composition Services
Media Development
Project Coordinator: Kristie Rees
Project Editor: Colleen Totz Diamond
Layout and Graphics: Ashley Chamberlain,
Acquisitions Editor: Katie Mohr Samantha K. Cherolis
Copy Editor: Melba Hopper Proofreaders: Laura L. Bowman,
Jessica Kramer
Technical Editor: Jan Karel Pieterse
Indexer: Rebecca Salerno
Editorial Manager: Jodi Jensen
Media Development Assistant Project
Manager: Jenny Swisher
Media Development Associate Producer:
Josh Frank
Editorial Assistant: Amanda Graham
Sr. Editorial Assistant: Cherie Case
Cartoons: Rich Tennant
(www.the5thwave.com)
Publishing and Editorial for Technology Dummies
Richard Swadley, Vice President and Executive Group Publisher
Andy Cummings, Vice President and Publisher
Mary Bednarek, Executive Acquisitions Director
Mary C. Corder, Editorial Director
Publishing for Consumer Dummies
Diane Graves Steele, Vice President and Publisher
Composition Services
Debbie Stailey, Director of Composition Services
0011__550033669900--ffffiirrss..iinndddd vvii 44//1122//1100 1111::2200 PPMM
Contents at a Glance
Introduction ................................................................ 1
Part I: Introducing VBA ............................................. 11
Chapter 1: What Is VBA? .................................................................................................13
Chapter 2: Jumping Right In ...........................................................................................23
Part II: How VBA Works with Excel ............................. 35
Chapter 3: Working In the Visual Basic Editor .............................................................37
Chapter 4: Introducing the Excel Object Model ...........................................................55
Chapter 5: VBA Sub and Function Procedures ............................................................69
Chapter 6: Using the Excel Macro Recorder ................................................................81
Part III: Programming Concepts .................................. 93
Chapter 7: Essential VBA Language Elements ..............................................................95
Chapter 8: Working with Range Objects .....................................................................115
Chapter 9: Using VBA and Worksheet Functions.......................................................129
Chapter 10: Controlling Program Flow and Making Decisions.................................141
Chapter 11: Automatic Procedures and Events .........................................................161
Chapter 12: Error-Handling Techniques .....................................................................183
Chapter 13: Bug Extermination Techniques ...............................................................197
Chapter 14: VBA Programming Examples...................................................................211
Part IV: Communicating with Your Users ................... 233
Chapter 15: Simple Dialog Boxes .................................................................................235
Chapter 16: UserForm Basics .......................................................................................253
Chapter 17: Using UserForm Controls ........................................................................269
Chapter 18: UserForm Techniques and Tricks ..........................................................289
Chapter 19: Accessing Your Macros through the User Interface ............................313
Part V: Putting It All Together .................................. 331
Chapter 20: Creating Worksheet Functions — and Living to Tell about It .............333
Chapter 21: Creating Excel Add-Ins .............................................................................349
0022__550033669900--ffttoocc..iinndddd vviiii 44//1122//1100 1111::2200 PPMM
Part VI: The Part of Tens .......................................... 361
Chapter 22: Ten VBA Questions (And Answers) .......................................................363
Chapter 23: (Almost) Ten Excel Resources ................................................................367
Index ...................................................................... 371
0022__550033669900--ffttoocc..iinndddd vviiiiii 44//1122//1100 1111::2200 PPMM