Tải bản đầy đủ (.pdf) (61 trang)

Mastering Excel 2003 Programming with VBA phần 1 pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.85 MB, 61 trang )

4281book.fm Page i Sunday, February 29, 2004 5:12 PM
Mastering
Excel 2003 Programming
with VBA
This page intentionally left blank
4281book.fm Page iii Sunday, February 29, 2004 5:12 PM
Mastering

Excel 2003 Programming
with VBA
Steven M. Hansen
San Francisco London
4281book.fm Page iv Sunday, February 29, 2004 5:12 PM
Associate Publisher: Joel Fugazzotto
Acquisitions Editor: Tom Cirtin
Developmental Editor: Brianne Agatep
Production Editor: Susan Berge
Technical Editor: Acey Bunch
Copyeditor: Rebecca C. Rider
Compositor: Jeff Wilson, Happenstance Type-O-Rama
Proofreaders: Laurie O’Connell, Amy J. Rasmussen, Nancy Riddiough
Indexer: Ted Laux
Book Designer: Maureen Forys, Happenstance Type-O-Rama
Cover Designer: Design Site
Cover Illustrator: Tania Kac, Design Site
Copyright © 2004 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved. No part of this publication may be
stored in a retrieval system, transmitted, or reproduced in any way, including but not limited to photocopy, photograph, magnetic, or other record,
without the prior agreement and written permission of the publisher.
Library of Congress Card Number: 2003115586
ISBN: 0-7821-4281-8


SYBEX and the SYBEX logo are either registered trademarks or trademarks of SYBEX Inc. in the United States and/or other countries.
Mastering is a trademark of SYBEX Inc.
Screen reproductions produced with FullShot 99. FullShot 99 © 1991-1999 Inbit Incorporated. All rights reserved.
FullShot is a trademark of Inbit Incorporated.
TRADEMARKS: SYBEX has attempted throughout this book to distinguish proprietary trademarks from descriptive terms by following the cap-
italization style used by the manufacturer.
The author and publisher have made their best efforts to prepare this book, and the content is based upon final release software whenever possible.
Portions of the manuscript may be based upon pre-release versions supplied by software manufacturer(s). The author and the publisher make no
representation or warranties of any kind with regard to the completeness or accuracy of the contents herein and accept no liability of any kind
including but not limited to performance, merchantability, fitness for any particular purpose, or any losses or damages of any kind caused or alleged
to be caused directly or indirectly from this book.
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
4281book.fm Page v Sunday, February 29, 2004 5:12 PM
Dedicated to my
mom to whom I am
indebted for instilling
in me a hefty dose of
curiosity, patience,
and independence
4281book.fm Page vi Sunday, February 29, 2004 5:12 PM
Acknowledgments
Writing a book of
this type is an interesting activity. As you may suspect, it is also very solitary.
Thankfully, though, you don’t have to read the raw manuscript. Many people contributed their spe-
cial talents to translate my thoughts into the cohesive whole you are holding.
My development editor, Brianne Agatep, kept me focused, fed my ego with praise, and managed the
entire project. Brianne, you are tops on my list. Thank you for all of your hard work and encouragement.
The production editor handles the schedule for the project and makes sure everything runs
smoothly. My production editor was Susan Berge. Susan did a wonderful job of keeping everything

moving according to schedule.
My technical editor was Acey Bunch. Acey had a tough job—he not only had to read each chapter
to check it for technical accuracy, he also tested all of the listings to see if they all worked as they
should. Acey went above and beyond by performing all his required duties and providing me with
many useful comments and suggestions.
In my opinion, the copy editor, Rebecca Rider, had the toughest job. She had to make sure each
chapter lived up to the grammatical standards of my publisher. You should see all of the edits she
made! Thank you, Rebecca, for making me look like I know how to put together a sentence.
I must extend a special thanks to the acquisitions editor, Tom Cirtin, for his help in getting me involved
with this book and all of the constructive feedback he provided throughout the project.
Thanks as well to the rest of the staff at Sybex including Amy Romanoff who was responsible for
the cover copy; the folks at Happenstance Type-O-Rama who laid out the book; proofreaders Laurie
O’Connell, Amy J. Rasmussen, and Nancy Riddiough; and indexer Ted Laux.
I would also like to thank my former colleagues Chris Kunicki and Charles Maxson. Chris and
Charles run the OfficeZealot.com website—a useful site for VBA/Microsoft Office developers.
Chris and Charles gave me several ideas and provided some useful information about XML and Smart
Document functionality.
No one knows how much work it is to write a book (especially on top of your “regular” job) better
than the close friends and family of an author. The process of writing this book had a profound
impact on those closest to me. I mean, does a five-year-old really understand why you suddenly can’t
play with him as much as you used to? Cole, it tore at my heart to leave the house to go write on the
weekends rather than play with you.
Do you know how movie stars and producers always emphasize their thanks to their spouses when
they receive an award for their hard work? I now understand and appreciate this practice. Any significant
undertaking is hard work, but all of the satisfaction of completing it goes to the person doing the work,
not the person who has to stay at home and take up the slack of the “I’m too busy” spouse. Therefore,
I would like to end by extending a heartfelt thank you to my wife Kelly for managing without me while I
was writing. You put your needs on hold through this whole process to care for our newborn daughter
Kennedy and our son Cole without much help from me. Thank you with all of my heart.
4281book.fm Page vii Sunday, February 29, 2004 5:12 PM

Contents at a Glance
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Part 1 • Introduction to Excel Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Chapter 1 • Excel as a Development Platform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3
Chapter 2 • Getting to Know Your Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Chapter 3 • Getting Started with VBA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Chapter 4 • Debugging Tactics that Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Part 2 • Mastering the Excel Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Chapter 5 • Exploring the Application Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Chapter 6 • Working with the Workbook Object . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Chapter 7 • Winning with Worksheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Chapter 8 • The Most Important Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Chapter 9 • Practical Range Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Chapter 10 • Exploring Other Excel Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Part 3 • Advanced Excel Programming Techniques . . . . . . . . . . . . . . . . . . . . . . . . 233
Chapter 11 • Developing Class Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Chapter 12 • Adding User Personalization to Your Application . . . . . . . . . . . . . . . . 259
Chapter 13 • Excel Development Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Part 4 • Working with External Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Chapter 14 • Integrating with Other Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Chapter 15 • Incorporating Text Files in Your Solution . . . . . . . . . . . . . . . . . . . . . . 321
4281book.fm Page viii Sunday, February 29, 2004 5:12 PM
viii
CONTENTS AT A GLANCE
Chapter 16 • Dealing with Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Chapter 17 • XL(M) = XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
Part 5 • Enhancing the End User Experience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Chapter 18 • Basic User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Chapter 19 • Taking Control of Command Bars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Chapter 20 • User Form Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463

Chapter 21 • One Smart Cookie: Smart Documents with Excel 2003 . . . . . . . . . . . . 493
Chapter 22 • Application Deployment Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
4281book.fm Page ix Sunday, February 29, 2004 5:12 PM
Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Part 1 • Introduction to Excel Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Chapter 1 • Excel as a Development Platform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Who Develops in Excel? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
What Is an Excel Application? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
A Short Survey of Excel Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Why Use Excel? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Extend a Great Product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Millions of Potential Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
File � Send To… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Exploit the Knowledge Base . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Rapid Development. Really. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
It’s Not Rocket Science… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Stop When You See Red . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
A Reflection on the Learning Curve . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Chapter 2 • Getting to Know Your Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
One Exceptional Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11
Navigating Projects with the Project Explorer Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13
The Versatile Properties Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15
Capabilities of the Immediate Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16
Managing Your Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16
Adding New Components to Your Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16
Removing Components from Your Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
Importing and Exporting Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17

Optimizing Your Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18
Give Syntax Highlighting a Try . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18
Turn On All of the Code Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19
Helpful Editor Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20
Understated Features of the Code Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22
Object Inspection Using the Object Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24
Locating Objects with the Object Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26
Obtaining Help Has Never Been So Easy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26
Securing Your Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29
Quick Code Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31
Chapter 3 • Getting Started with VBA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Thinking Like a Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .33
4281book.fm Page x Sunday, February 29, 2004 5:12 PM
x
CONTENTS
VBA Building Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35
Modules—A Home for Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35
Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36
Variables Are the Elements You Interact With . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40
Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41
Declaring Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45
Variable Scope and Lifetime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45
Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48
Directing Your Program with Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49
Implementing Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49
Implementing Branching with If…Then . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53
Thinking Like a Computer Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .56
Choosing Actions with Select Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57

Basic Array Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59
Specifying the Index Range of an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .60
Objects—A Human-Friendly Way to Think of Bits and Bytes . . . . . . . . . . . . . . . . . . . . . . . . . .61
Methods and Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .62
Chapter 4 • Debugging Tactics that Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
A Bug Epidemic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65
Syntax Errors Are Your Friend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65
Run-Time Embarrassments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .67
Logical Errors Cause Gray Hair . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .68
Debugging Weapons in the VBE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69
Break Mode: For Fixing Things that Are Broken . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69
Reconnaissance Operations and Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73
Zero In on Problem Areas with Watches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .75
Clarify the Muddle with the Call Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .78
A Simple Debugging Methodology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .79
Correcting Run-Time Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .79
Debugging Logical Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81
Implementing Simple Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .82
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .84
Part 2 • Mastering the Excel Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Chapter 5 • Exploring the Application Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
A Bird’s-Eye View of the Application Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87
Display-Oriented Features You Have to Know . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88
Gaining Performance and Polish with ScreenUpdating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88
Keeping End Users Informed with the Status Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .90
Display-Oriented Features That Are Nice to Know . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .93
Convenient Excel Object Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .95
Common File Operations Simplified . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .96
Obtaining Filenames from a User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .96

Pick the Perfect Location with GetSaveAsFilename . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .100
4281book.fm Page xi Sunday, February 29, 2004 5:12 PM
xi
CONTENTS
Inspecting Your Operating Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .105
Two Useful Bonus Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .106
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .107
Chapter 6 • Working with the Workbook Object . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Walk before You Run: Opening and Closing Workbooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . .109
Is That Workbook Open? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114
Specifying Specific Collection Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .116
Untangle Links Programmatically (Part I) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .117
Plain Vanilla Workbook Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .123
Respond to User Actions with Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .125
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .130
Chapter 7 • Winning with Worksheets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Setting the Stage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133
Validating Your Worksheets before Using Them . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .136
Now You See It, Now You Don’t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138
Lock Up Your Valuables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .140
Managing Workbook Worksheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142
Adding and Deleting Worksheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142
Moving and Copying Worksheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .146
Expounding on Worksheet Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .148
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .150
Chapter 8 • The Most Important Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Referring to Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .153
Cells and Ranges with the Worksheet Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .155
Referring to Named Ranges Can Be Tricky . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .158
Finding My Way . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .163

Offset Is for Relative Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .164
Last but Not Least—Finding the End . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .167
Input Easy; Output Easier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .171
Output Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .172
Accepting Worksheet Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .178
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .179
Chapter 9 • Practical Range Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Data Mobility with Cut, Copy, and Paste . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .181
Find What You Are Seeking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183
Don’t Like It? Change It with Replace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .189
Would You Like Special Sauce with That? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .193
CurrentRegion: A Useful Shortcut . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .196
Sorting Lists Simplified . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .199
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .203
Chapter 10 • Exploring Other Excel Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Make a Good First Impression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205
Coloring Your World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205
4281book.fm Page xii Sunday, February 29, 2004 5:12 PM
xii
CONTENTS
Fiddling with Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .208
Interior Decorating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211
No Visa Required For These Borders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .214
Formatting Those Figures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .217
Chart Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .223
Creating a Chart from Scratch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .224
Chart Hunting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .228
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .232
Part 3 • Advanced Excel Programming Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Chapter 11 • Developing Class Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235

Class Modules Are for Creating Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .235
A Linguistic Foundation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .236
What’s the Big Deal? Why Use Objects? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .237
Classes Unite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .237
Classes Hide the Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .238
Classes Enable Collaboration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .238
Creating a Simple Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .238
Using Your Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .240
A Better, Smarter Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242
An Object Showdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .246
Collecting Your Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .248
Implementing More Accurate Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .252
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .258
Chapter 12 • Adding User Personalization to Your Application . . . . . . . . . . . . . . 259
The Need for Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .259
Class Semi-Specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .260
Planning the Plumbing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .261
Security Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .262
The Setting Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .262
Fake Is Fine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .270
Collect Those Setting Objects with Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .271
Pseudo? Says Who? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .276
Put Those Settings to Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .277
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .279
Chapter 13 • Excel Development Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Deactivate Activate; Don’t Select Select . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .281
Manage the Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .284
Design for Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .285
Tips for Creating Portable Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .286
Test the Water before Jumping In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .286

Remember Your Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .286
Think Like an Environmentalist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .287
4281book.fm Page xiii Sunday, February 29, 2004 5:12 PM
xiii
CONTENTS
Use Literal Values with Care . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .288
Use Syntax Highlighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .288
Manage Literal Values Centrally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .289
Smart Workbook Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .295
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .298
Part 4 • Working with External Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Chapter 14 • Integrating with Other Applications. . . . . . . . . . . . . . . . . . . . . . . . . 301
A Primer to Office Automation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .301
Expounding On Compound Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303
Crafting Compound Documents Programmatically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .305
OLE Is Great; Automation Is Better . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .308
Binding to a Class Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .309
An Automation Example: Presentation Automation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .312
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .319
Chapter 15 • Incorporating Text Files in Your Solution. . . . . . . . . . . . . . . . . . . . . 321
Versatile Simplicity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .321
Opening Text Files in Excel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .323
Importing Text Data onto a Worksheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .326
Automatic Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .328
The Old Standby—Copy/Paste . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .334
Opening Files Under the Covers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .337
Open for Business . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .337
File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .339
String Functions Offering Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .344
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .347

Chapter 16 • Dealing with Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Database Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .349
Developing Your Skills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .351
Native Excel Database Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .352
Excel, Meet My Database. Database, This Is Excel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .353
You Are an Advanced Player . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .355
Turbo Charge Your Data Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .361
Work Magic with ADO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .363
Make the Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .365
Ready, Set, Query! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .367
It’s Not Just about Retrieving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .371
I Like Treats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .374
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .380
Chapter 17 • XL(M) = XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
Y XML? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .383
XML in XL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .386
Easy XL XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .386
4281book.fm Page xiv Sunday, February 29, 2004 5:12 PM
xiv
CONTENTS
XL XML Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .388
4X
3
M
2
L
3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .393
XML VBA Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .394
A List Object Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .403

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .408
Part 5 • Enhancing the End User Experience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Chapter 18 • Basic User Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
User Interfaces in Excel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .411
Paleozoic Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .412
The Ubiquitous Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .413
Free to Choose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .415
Makin’ a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .419
Scrolling and Spinning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .421
Like a Kid in a Candy Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .423
The Ubiquitous Button Redux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .424
Combo Box Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .427
Judge a Book by Its Cover . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .429
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .432
Chapter 19 • Taking Control of Command Bars . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Taking Inventory with CommandBars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .435
Reflecting on a CommandBar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .439
The Bendy CommandBarControl Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .446
Visualization, Prioritization, and CommandBarControls . . . . . . . . . . . . . . . . . . . . . . . . . . . .448
Finding Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .448
Crafting Custom Command Bars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .450
Building a New Command Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .459
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .462
Chapter 20 • User Form Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
User Form Development Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .463
A Simple Form Walkthrough . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .465
Forms Are Meant to Be Shown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .469
Show First, Ask Questions Later . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .469
Load and Show . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .470
Classy Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .471

The Form Lifecycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .474
User Friendly Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .481
Primitive Password Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .489
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .491
Chapter 21 • One Smart Cookie: Smart Documents with Excel 2003 . . . . . . . . . . 493
Smart Document Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .493
Being Smart Has Benefits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .494
A Smart Prerequisite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .496
4281book.fm Page xv Sunday, February 29, 2004 5:12 PM
xv
CONTENTS
Key Components of Smart Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .496
Smart Document Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .497
A Smart Document Walkthrough . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .498
A Not So Smart Document (with Lots of Potential) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .498
XML Helps Make You Smart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .499
Implementing ISmartDocument Makes You Smarter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .501
Connect the Dots with a Manifest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .523
See How Smart It Is . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .524
Troubleshooting Smart Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .525
Smart Document Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .526
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .526
Chapter 22 • Application Deployment Strategies . . . . . . . . . . . . . . . . . . . . . . . . . 529
Choose the Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .529
I Like Vanilla . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .530
Repeat Your Success with Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .530
Blend in with Add-Ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .535
Managing Change . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .539
Employing Centralized Template Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .539
Implementing Version Awareness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .541

Fear Not When Things Go Wrong . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .550
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .554
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
This page intentionally left blank
4281book.fm Page xvii Sunday, February 29, 2004 5:12 PM
Introduction
Do you enjoy helping
people? Are you seeking the warm satisfaction associated with cre-
ating value? If you learn how to develop applications based in Excel, you will be a happier person
because you’ll have the opportunity to help people and create value by creating applications that
allow people to work much more efficiently.
I was talking to an Excel guru the other day. We were reflecting on what we have enjoyed most
about our consulting careers. We both agreed that the most satisfying aspect of our career is the
fulfillment we experience after solving some problem and witnessing the genuine satisfaction and
elation a client expresses in the result.
Because so many people spend their whole day working with Excel and because it is so easy to pro-
gram in Excel using Visual Basic for Applications (VBA), you still have a lot of opportunity to
develop useful applications that create value. This book will teach you how to develop such appli-
cations based in Excel using VBA and, according to the preceding logic, lead you to happiness.
Philosophy
There are many approaches to presenting technical information. One approach is to write a volu-
minous tome of dry, factual information and leave it up to the reader to figure out how to apply
the information or decipher the useful content from the not-so-useful content. You may have
come across these types of books before. They are much like the information in help files, except
rearranged.
At the other extreme, you can write to the dummies. That is, you can write a book that can be
read without much mental activity on the part of the reader. In order to write a book like this, you
must stick to trivial content and trivial examples. It is then up to the reader to figure out how to
make the mental leap from the trivial examples presented in the book to the complexities presented
by real-world problems. From my own observations, it seems as though most people don’t make

the leap (they buy another book instead). I’ve always thought that in order to learn, you have to
exercise the brain. No pain, no gain, right?
Hopefully I’ve achieved something of a middle ground with this book. I did not cover every
nook and cranny associated with Excel development. Nor did I hesitate to get in deep on the topics
that are critical to developing Excel applications. I picked the topics that have been most important
to my development experience and presented the necessary facts about them along with illustra-
tions of useful ways to apply them. Finally, I hope that I’ve sprinkled this book with just enough
good-natured reflection and commentary to make this a more colorful read than other program-
ming books. If this book were a television, I would like to think of it as an HDTV.
4281book.fm Page xviii Sunday, February 29, 2004 5:12 PM
xviii
INTRODUCTION
Finally, I’ve attempted to inject a significant amount of real-world examples in this book. In order
to become proficient in Excel development, you’ll need to develop your own collection of generic
procedures or building blocks. By building on your prebuilt foundation, so to speak, you can churn
out applications with less effort and in a shorter amount of time. In order to help you acquire your
own foundation of generic routines, I present many useful procedures that illustrate the topic at hand.
I use many of these procedures in nearly every application I build.
Who Are You?
This book was written to two primary audiences: Excel power users who want to learn how to pro-
gram, and programmers who want to learn about the Excel object model. That said, anyone who
wants to acquire Excel VBA development skills will find useful information in this book. I should
also add that this book was written with a corporate audience in mind. If you aren’t part of that audi-
ence, fear not, you won’t have any trouble understanding the material, you’ll just find that many of
the examples are business oriented.
Excel power users are people who use Excel on a daily basis for their job, are comfortable using
it, and are fairly knowledgeable about Excel’s functionality. If you are part of this group of readers,
you may be interested in acquiring some Excel development skills so that you can automate aspects
of your job. I recommend that this group read this book cover to cover, paying particular attention
to the first two parts (through Chapter 10).

Programmers bring a different perspective to the table. Programmers can span the spectrum in
terms of programming skill and general knowledge of Excel. If you do not have experience with
Visual Basic, I would recommend that you read the first two parts (Chapters 1–10) and then those
chapters applicable to your situation. If you have a fair amount of experience with Visual Basic, I’d
advise you to focus primarily on the second part (Chapters 5–10), which deals with the Excel object
model, and then read the chapters applicable to your situation.
Regardless of your background, everyone should pay particular attention to Part 2, which covers
the Excel object model. These chapters are critical to developing any useful Excel development skills.
The remaining sections of the book are organized by high-level task with the exception of Part 3,
which focuses on slightly more advanced topics such as developing classes and best practices. If you need
to integrate Excel with other applications or use data from an external data source, you’ll find coverage
of this material in Part 4. Finally, the book ends with Part 5, which covers user interface development.
Version Differences Are Not Critical
I developed the examples in this book using Excel 2003. For the most part, however, you can apply
the material presented to previous versions of Excel back to Excel 97. If you are working with an older
version of Excel, you may occasionally see a reference to an object in this book that is not available
in your version. Generally, if you’re familiar with the incremental functionality offered by each version
of Excel, you’ll already have a good idea of what type of programmatic differences there are between
versions. These differences are minor in the grand scheme of things. In fact, I still approach projects
the same way today as I did with Excel 97. For that matter, you could also use many of the pro-
cedures in this book with Excel 5.0. In fact, I wrote some of the procedures presented in this book
using Excel 5.0 in 1996.
4281book.fm Page xix Sunday, February 29, 2004 5:12 PM
xix
INTRODUCTION
However, two chapters contain information specific to Excel 2003: the chapter concerning Excel
XML functionality (Chapter 17) and the chapter covering the development of Smart Documents
(Chapter 21). As far as I’m concerned, XML functionality and Smart Document technology are the
two most exciting improvements in Excel 2003.
Website Extras

All of the examples presented in this book are available on the web at
www.dakotatechgroup.com/
ExcelVBA
. In addition to the example workbooks, I’ll post any updates, corrections, and other useful
information related to this book. You can also obtain the example workbooks from Sybex’s website
(
www.sybex.com
).
Your Feedback Is Important
Before I move on, I’d like to say that your feedback is important to me. Although I might not be able
to respond to all of your comments and questions, I guarantee that I’ll read them all. Please feel free to
e-mail your questions and comments to me at

.
I hope you enjoy reading this book as much as I enjoyed writing it. Well, as much as it is possible
to enjoy reading a technical book anyway. Happy reading.…
This page intentionally left blank
4281book.fm Page 1 Sunday, February 29, 2004 5:12 PM
Part
1
Introduction
to Excel
Development
In this section:
◆ Chapter 1: Excel as a Development Platform
◆ Chapter 2: Getting to Know Your Environment
◆ Chapter 3: Getting Started with VBA
◆ Chapter 4: Debugging Tactics that Work
This page intentionally left blank
4281book.fm Page 3 Sunday, February 29, 2004 5:12 PM

Chapter 1
Excel as a Development Platform
I often find myself explaining what it means when I say I am an Excel developer. People have
heard of Java developers, C/C++ developers, Visual Basic developers, web developers, and many
other various technologies that people develop in, but an Excel developer?
This chapter introduces you to what it means to be an Excel developer and why Microsoft Excel
makes such an interesting and useful development platform for many types of applications.
Who Develops in Excel?
When I was younger, I remember wanting to be many different things: a dolphin trainer, a professional
hockey player, and a pilot, to name a few. I also went through a phase when I thought it would be cool
to be a programmer (except as a kid I wanted to develop games, not boring business programs). Then
I grew up and my priorities changed. In fact, in my first “real” career job, I was a financial analyst. At
no point in my life did I plan on becoming an Excel developer; it just happened. The factors that drove
me to become one are interesting and, I believe, quite common in the corporate world.
First, most analysts, and many other “knowledge workers” for that matter, live and breathe spread-
sheets. I was no different in that regard. I figured the better I understood all of the features and func-
tions available in Excel, the better I would become at my job as it would enable me to analyze data
more efficiently and with fewer mistakes.
Second, the analytical process tends to be a very tedious affair. Generally, you need to pull data
from disparate systems and organize it in a way that provides meaning. Then repeat, and repeat, and
repeat as reporting cycles dictate. You have daily tasks, weekly tasks, monthly tasks, and so on. You
need to work quickly and accurately—two characteristics that are generally diametrically opposed.
The greater the pressure to finish something, the easier it is to make mistakes. I felt that I spent more
time collecting and formatting data then I did analyzing or thinking critically about the results. To
me, this didn’t seem right. There had to be a better way.
Third, I had a manager who recognized my interests and understood the potential productivity
gains that could be had via analytical automation—that is, automating the analytical process. This
manager understood that though it takes a little more time upfront to build solutions rather than just
spreadsheets, the time could be easily recouped with each reporting cycle.
4281book.fm Page 4 Sunday, February 29, 2004 5:12 PM

4
CHAPTER 1 EXCEL AS A DEVELOPMENT PLATFORM
Finally, I had a genuine interest in technology in general. I enjoyed learning about the different
ways I could employ technology to make my life easier. I found that employing technology to make
my job less tedious had immediate rewards that served to reinforce my interest.
The result of all of these factors was that after a few years as a financial analyst, I came to realize
that my true calling was as a developer and not just any developer. I was an Excel developer, a devel-
oper specializing in developing analytical applications.
What Is an Excel Application?
First of all, I should define what I mean by an Excel application. By Excel application, I mean a
spreadsheet that you have developed using Excel and VBA for use by yourself or other users. VBA
stands for Visual Basic for Applications, which is essentially Visual Basic that has been modified to
run within other host applications such as Excel or Microsoft Word. Excel applications contain VBA
code that may perform one or more of the following tasks:
Automate tedious processes. Many times, once you create a useful spreadsheet, in order to
maintain it, you need to perform a set of tasks in a specific order on a frequent basis. Often, you
can automate such simple tasks using Excel’s macro recorder. For more complex tasks you need to
write VBA code.
Enhance the user experience. The experience and comfort level of people using Excel varies
widely. One way to spread the wealth of a useful spreadsheet is to incorporate a user interface into
the workbook that can serve to guide the less experienced users and help the more experienced
users work more efficiently.
Hide the underlying complexity of a spreadsheet. One of the quickest ways to limit the use-
fulness of an otherwise excellent spreadsheet is to expose people to numerous worksheets contain-
ing an expanse of calculations or data. You can use VBA to manage the layout and display of a
workbook so that users don’t get lost in your otherwise brilliant work.
Integrate with another application or database. Most corporate data is locked in other appli-
cations or databases. By enabling Excel to integrate with these sources of data, you can empower
users to analyze this data with much greater efficiency.
Prevent the modification of certain aspects of a spreadsheet. Once you have laboriously cre-

ated your spreadsheet, you are apt to have sections that others could modify to their own detri-
ment. To prevent this from occurring, you can use various techniques to allow others to interact
with the workbook only in the ways that you intended.
Some Excel applications contain very little code. These applications are models that are thought-
fully and consciously designed for the task at hand by applying sound design methodologies to the
design of a spreadsheet. Although you’ll find a sprinkling of advice regarding general design meth-
odologies throughout this book, I focus more on teaching you how to use VBA rather than how to
design efficient spreadsheets.

×