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

excel 2002 power programming with vba phần 1 docx

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 (4.04 MB, 99 trang )

















Develop Powerful, User-Oriented Excel 2002 Applications
With this unique guide, renowned spreadsheet authority John Walkenbach invites
you to harness the full potential of Excel—and join the elite group of users and
developers who truly understand what the product is capable of. Offering plenty
of detailed, well-thought-out examples, he walks you through the basics of Excel
application development and VBA programming—and then gives you a lucid,
wide-ranging seminar on advanced programming techniques and application
development issues. It’s all you need to create user-oriented custom applications
that leverage the power of Excel 2002.
Your Road Map to Excel Application Development
• Take Excel to the next level with formula tricks and techniques
• Work with VBA sub procedures and function procedures
• Create stellar UserForms as well as custom dialog box alternatives
• Develop applications that maximize the power of PivotTables and charts
• Incorporate event-handling and interactions with other applications


• Build user-friendly toolbars, menus, and help systems
• Manipulate files and Visual Basic components and understand class modules
• Get advice on compatibility issues and answers to frequently asked Excel
programming questions
Excel 2002 Power
Programming with VBA
$49.99 USA
$74.99 CANADA
£39.99 UK incl. VAT
Excel 2002 Power Programming with VBA
Walkenbach
Reader Level
Intermediate to Advanced
Shelving Category
Programming/Spreadsheets
Power Utility Pak trial
and more on CD-ROM
Visit us at mandtbooks.com
CD-ROM
included
PROFESSIONAL MINDWARE
TM
• A trial version of the author’s
award-winning Power Utility
Pak 2000
• A demo of the author’s
Sound-Proof 2000
• Over 175 example Excel
workbooks from the book
BONUS CD-ROM

INCLUDES
Excel 2002 Power
Programming with VBA
John Walkenbach
Author of Excel 2002 Bible
“An outstanding reference. . . . If you use Excel,
you need this book.”
— Microsoft OfficePRO magazine on the previous edition
John Walkenbach is a
principal of JWalk and Associates, Inc., a
consulting firm that specializes in
spreadsheet application development,
and is the author of more than two
dozen books, including, most
recently, Excel 2002 Bible and Excel
2002 Formulas. He maintains “The
Spreadsheet Page,” a popular Web
resource at www.j-walk.com/ss.
,!7IA7G4-fehjjh!:p;o;t;T;T
ISBN 0-7645-4799-2
*85555-AGBGEe
www.mandtbooks.com
System Requirements: Pentium PC with
Windows 98 or greater; 32 MB RAM; Microsoft
Excel 2002. See About the CD appendix for
details and complete system requirements.
4799-2 Cover_rb3.qxp 5/31/01 3:06 PM Page 1
Excel 2002 Power
Programming with VBA
4799-2 FM.F 6/11/01 1:12 PM Page i

4799-2 FM.F 6/11/01 1:12 PM Page ii
Excel 2002 Power
Programming with VBA
John Walkenbach
M&T Books
An imprint of Hungry Minds, Inc.
New York, NY ✦ Cleveland, OH ✦ Indianapolis, IN
4799-2 FM.F 6/11/01 1:12 PM Page iii
Excel 2002 Power Programming with VBA
Published by
M&T Books
An imprint of Hungry Minds, Inc.
909 Third Avenue
New York, NY 10022
www.hungryminds.com
Copyright © 2001 Hungry Minds, Inc. All rights
reserved. No part of this book, including interior
design, cover design, and icons, may be reproduced
or transmitted in any form, by any means
(electronic, photocopying, recording, or otherwise)
without the prior written permission of the
publisher.
Library of Congress Control Number 2001089315
ISBN: 0-7645-4799-2
Printed in the United States of America
10 9 8 7 6 5 4 3 2 1
1O/SY/QW/QR/IN
Distributed in the United States by Hungry Minds,
Inc.
Distributed by CDG Books Canada Inc. for Canada;

by Transworld Publishers Limited in the United
Kingdom; by IDG Norge Books for Norway; by IDG
Sweden Books for Sweden; by IDG Books Australia
Publishing Corporation Pty. Ltd. for Australia and
New Zealand; by TransQuest Publishers Pte Ltd. for
Singapore, Malaysia, Thailand, Indonesia, and Hong
Kong; by Gotop Information Inc. for Taiwan; by ICG
Muse, Inc. for Japan; by Intersoft for South Africa;
by Eyrolles for France; by International Thomson
Publishing for Germany, Austria, and Switzerland;
by Distribuidora Cuspide for Argentina; by LR
International for Brazil; by Galileo Libros for Chile;
by Ediciones ZETA S.C.R. Ltda. for Peru; by WS
Computer Publishing Corporation, Inc., for the
Philippines; by Contemporanea de Ediciones for
Venezuela; by Express Computer Distributors for
the Caribbean and West Indies; by Micronesia
Media Distributor, Inc. for Micronesia; by Chips
Computadoras S.A. de C.V. for Mexico; by Editorial
Norma de Panama S.A. for Panama; by American
Bookshops for Finland.
For general information on Hungry Minds’ products
and services please contact our Customer Care
department within the U.S. at 800-762-2974, outside
the U.S. at 317-572-3993 or fax 317-572-4002.
For sales inquiries and reseller information,
including discounts, premium and bulk quantity
sales, and foreign-language translations, please
contact our Customer Care department at
800-434-3422, fax 317-572-4002 or write to Hungry

Minds, Inc., Attn: Customer Care Department, 10475
Crosspoint Boulevard, Indianapolis, IN 46256.
For information on licensing foreign or domestic
rights, please contact our Sub-Rights Customer
Care department at 212-884-5000.
For information on using Hungry Minds’ products
and services in the classroom or for ordering
examination copies, please contact our Educational
Sales department at 800-434-2086 or fax
317-572-4005.
For press review copies, author interviews, or
other publicity information, please contact our
Public Relations department at 317-572-3168 or fax
317-572-4168.
For authorization to photocopy items for
corporate, personal, or educational use, please
contact Copyright Clearance Center, 222 Rosewood
Drive, Danvers, MA 01923, or fax 978-750-4470.
LIMIT OF LIABILITY/DISCLAIMER OF W
ARRANTY: THE PUBLISHER AND AUTHOR HAVE USED THEIR
BEST EFFORTS IN PREPARING THIS BOOK. THE PUBLISHER AND AUTHOR MAKE NO
REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE
CONTENTS OF THIS BOOK AND SPECIFICALLY DISCLAIM ANY IMPLIED WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. THERE ARE NO WARRANTIES WHICH
EXTEND BEYOND THE DESCRIPTIONS CONTAINED IN THIS PARAGRAPH. NO WARRANTY MAY BE
CREATED OR EXTENDED BY SALES REPRESENTATIVES OR WRITTEN SALES MATERIALS. THE
ACCURACY AND COMPLETENESS OF THE INFORMATION PROVIDED HEREIN AND THE OPINIONS
STATED HEREIN ARE NOT GUARANTEED OR WARRANTED TO PRODUCE ANY PARTICULAR RESULTS,
AND THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY
INDIVIDUAL. NEITHER THE PUBLISHER NOR AUTHOR SHALL BE LIABLE FOR ANY LOSS OF PROFIT OR

ANY OTHER COMMERCIAL DAMAGES, INCLUDING BUT NOT LIMITED TO SPECIAL, INCIDENTAL,
CONSEQUENTIAL, OR OTHER DAMAGES. FULFILLMENT OF EACH COUPON OFFER IS THE SOLE
RESPONSIBILITY OF THE OFFEROR.
Trademarks: Professional Mindware is a trademark or registered trademark of Hungry Minds. Microsoft is a
registered trademark or trademark of Microsoft Corporation. All other trademarks are property of their
respective owners. Hungry Minds, Inc. is not associated with any product or vendor mentioned in this book.
is a trademark of is a trademark of
Hungry Minds, Inc. Hungry Minds, Inc.
4799-2 FM.F 6/11/01 1:12 PM Page iv
About the Author
John Walkenbach is a leading authority on spreadsheet software and is principal of
JWalk and Associates Inc., a small, San Diego-based consulting firm that specializes
in spreadsheet application development. John is the author of more than two dozen
spreadsheet books and has written more than 300 articles and reviews for a variety
of publications, including PC World, InfoWorld, Windows, and PC/Computing. He also
maintains The Spreadsheet Page, a popular Internet Web site (
www.j-walk.com/ss),
and is the developer of Power Utility Pak, an award-winning add-in for Microsoft
Excel. John graduated from the University of Missouri and earned a master’s and a
Ph.D. from the University of Montana.
In addition to computers and spreadsheet software, John’s other interests include
guitar, music, novels, digital photography, and puttering around in the garden.
4799-2 FM.F 6/11/01 1:12 PM Page v
Credits
Acquisitions Editor
Greg Croy
Project Editor
Susan Christophersen
Technical Editor
Bill Manville

Copy Editor
Jennifer Mario
Editorial Manager
Kyle Looper
Project Coordinator
Dale White
Graphics and Production Specialists
Amy Adrian
Joyce Haughey
LeAndra Johnson
Betty Schulte
Brian Torwelle
Stephanie D. Jumper
Quality Control Technicians
Andy Hollandbeck
Carl Pierce
Dwight Ramsey
Charles Spencer
Senior Permissions Editor
Carmen Krikorian
Media Development Specialist
Travis Silvers
Media Development Coordinator
Marisa Pearman
Proofreading and Indexing
TECHBOOKS Production Services
Cover Image
© Noma/Images.com
4799-2 FM.F 6/11/01 1:12 PM Page vi
This one’s for Michelle.

4799-2 FM.F 6/11/01 1:12 PM Page vii
4799-2 FM.F 6/11/01 1:12 PM Page viii
Preface
W
elcome to Excel 2002 Power Programming with VBA. If your job involves
developing spreadsheets that others will use— or if you simply want to
get the most out of Excel — you’ve come to the right place.
Why I Wrote This Book
Quite a few advanced Excel books are available, but this book is still the only one
that deals with spreadsheet application development from a larger perspective.
VBA is just one component (albeit a fairly large component) of application develop-
ment. Excel is an extremely deep software product: It has many interesting features
that lurk in the background, unbeknownst to the typical user. And you can use
some of the well-known features in novel ways.
Millions of people throughout the world use Excel. I monitor the spreadsheet-related
newsgroups on the Internet, and it’s very clear to me that people need (and want)
help in the areas that this book covers. My guess is that only five percent of Excel
users really understand what the product is capable of. In this book, I attempt to
nudge you into that elite company. Are you up to it?
What You Need to Know
This is not a book for beginning Excel users. If you have no experience with Excel, I
recommend that you read either of the following books:
✦ Excel 2002 for Windows For Dummies, by Greg Harvey, is written for users who
want to know just enough to get by, and want to be entertained in the process.
✦ Excel 2002 Bible (by yours truly) provides comprehensive coverage of all the
features of Excel. It is meant for users of all levels.
To get the most out of this book, you should be a relatively experienced Excel user.
I didn’t spend much time writing basic how-to information. In fact, I assume that
you know the following:
✦ How to create workbooks, insert sheets, save files, and so on

✦ How to navigate through a workbook
✦ How to use the menus and shortcut menus
4799-2 FM.F 6/11/01 1:12 PM Page ix
x
Preface
✦ How to manage Excel’s toolbars
✦ How to enter formulas
✦ How to use Excel’s worksheet functions
✦ How to name cells and ranges
✦ How to use basic Windows features, such as file management techniques and
the clipboard
If you don’t know how to perform the preceding tasks, you may find some of this
material over your head, so consider yourself warned. If you’re an experienced
spreadsheet user who hasn’t used Excel 2002, Chapter 2 presents a short overview
of what this product offers.
What You Need to Have
To make the best use of this book, you need a copy of Excel. Although the book was
written with Excel 2002 in mind, most of the material also applies to Excel 2000 and
Excel 97. If you use an earlier version of Excel, you’re reading the wrong book. Most
of the material in this book also applies to Excel for Macintosh. However, I did no
compatibility testing with the Mac version, so you’re on your own.
Any computer system that can run Windows will suffice, but you’ll be much better
off with a fast Pentium-based machine with plenty of memory. Excel is a large pro-
gram, and using it on a slower system or a system with minimal memory can be
extremely frustrating.
I recommend using a high-resolution video driver (800 × 600 is okay, 1024 × 768 is
excellent, and 1600 × 1024 is sheer heaven). A standard VGA resolution will do in a
pinch, but it just doesn’t let you see enough on-screen.
To make use of the examples on the companion CD, you will also need a CD-ROM
drive.

Conventions in This Book
Take a minute to skim this section and learn some of the typographic conventions
used throughout this book.
4799-2 FM.F 6/11/01 1:12 PM Page x
xi
Preface
Keyboard conventions
You need to use the keyboard to enter data. In addition, you can work with menus
and dialog boxes directly from the keyboard —a method you may find easier if your
hands are already positioned over the keys.
Input
Input that you type from the keyboard appears in boldface — for example, enter
=SUM(B2: B50) into cell B51.
More lengthy input usually appears on a separate line in a monospace font. For
example, I may instruct you to enter the following formula:
=VLOOKUP(STOCKNUMBER,PRICELIST,2)
VBA code
This book contains many snippets of VBA code, as well as complete procedure
listings. Each listing appears in a monospace font; each line of code occupies a
separate line. (I copied these listings directly from the VBA module and pasted
them into my word processor.) To make the code easier to read, I often use one or
more tabs to create indentations. Indentation is optional, but it does help to delin-
eate statements that go together.
If a line of code doesn’t fit on a single line in this book, I use the standard VBA line
continuation sequence: At the end of a line, a space followed by an underscore
character indicates that the line of code extends to the next line. For example, the
following two lines are a single line of code:
If Right(ActiveCell, 1) = “!” Then ActiveCell _
= Left(ActiveCell, Len(ActiveCell) - 1)
You can enter this code either on two lines, exactly as shown, or on a single line

without the underscore character.
Functions, filenames, and named ranges
Excel’s worksheet functions appear in uppercase monospace font, like so: “Enter a
SUM formula in cell C20.” VBA procedure names, properties, methods, and objects
appear in monospace font: “Execute the
GetTotals procedure.” I often use mixed
upper- and lowercase to make these names easier to read.
Mouse conventions
If you’re reading this book, you’re well versed in mouse usage. The mouse terminol-
ogy I use is all standard fare: pointing, clicking, right-clicking, dragging, and so on.
4799-2 FM.F 6/11/01 1:12 PM Page xi
xii
Preface
What the Icons Mean
Throughout the book, I’ve used icons in the left margin to call your attention to
points that are particularly important.
I use this icon to indicate that the material discussed is new to Excel 2002. If
you’re developing an application that will be used for earlier versions of Excel, pay
particular attention to these icons.
I use Note icons to tell you that something is important — perhaps a concept that
may help you master the task at hand or something fundamental for understand-
ing subsequent material.
Tip icons indicate a more efficient way of doing something or a technique that
may not be obvious.
These icons indicate that an example file is on the companion CD-ROM (see
“About the Companion CD-ROM,” later in the introduction). This CD holds many of
the examples that I cover in the book, as well as a trial copy of my popular Power
Utility Pak software.
I use Caution icons when the operation that I’m describing can cause problems if
you’re not careful.

I use the Cross Reference icon to refer you to other chapters that have more to
say on a subject.
How This Book Is Organized
The chapters of this book are grouped into seven main parts. In addition, I’ve
included a few appendixes that provide supplemental information.
Part I: Some Essential Background
In this part, I set the stage for the rest of the book. Chapter 1 presents a brief history
of spreadsheets so that you can see how Excel fits into the big picture. In Chapter 2,
I offer a conceptual overview of Excel 2002 — quite useful for experienced spread-
sheet users who are switching to Excel. In Chapter 3, I cover the essentials of
formulas, including some clever techniques that may be new to you. Chapter 4
covers the ins and outs of the various files used and generated by Excel.
Cross-
Reference
Caution
On the
CD-ROM
Tip
Note
New
Feature
4799-2 FM.F 6/11/01 1:12 PM Page xii
xiii
Preface
Part II: Excel Application Development
This part consists of just two chapters. In Chapter 5, I broadly discuss the concept
of a spreadsheet application. Chapter 6 goes into more detail and covers the steps
typically involved in a spreadsheet application development project.
Part III: Understanding Visual Basic for Applications
Chapters 7 through 11 make up Part III, and these chapters include everything you

need to know to learn VBA. In this part, I introduce you to VBA, provide program-
ming fundamentals, and detail how to develop VBA subroutines and functions.
Chapter 11 contains tons of useful VBA examples.
Part IV: Working with UserForms
The four chapters in this part cover custom dialog boxes (also known as UserForms).
Chapter 12 presents some built-in alternatives to creating custom UserForms.
Chapter 13 provides an introduction to UserForms and the various controls you
can use. Chapters 14 and 15 present many examples of custom dialog boxes, rang-
ing from basic to advanced.
Part V: Advanced Programming Techniques
Part V covers additional techniques that are often considered advanced. The first
three chapters discuss how to develop utilities and how to use VBA to work with
pivot tables and charts. Chapter 19 covers the topic of event-handling, which
enables you to execute procedures automatically when certain events occur.
Chapter 20 discusses various techniques that you can use to interact with other
applications (such as Word). Chapter 21 concludes Part V with an in-depth discus-
sion of creating add-ins.
Part VI: Developing Applications
The chapters in Part VI deal with important elements of creating user-oriented
applications. Chapters 22 and 23 provide information on creating custom toolbars
and menus. Chapter 24 presents several different ways to provide online help for
your applications. In Chapter 25, I present some basic information about develop-
ing user-oriented applications, and I describe such an application in detail.
Part VII: Other Topics
The five chapters in Part VII cover additional topics that you may find helpful.
Chapter 26 presents information regarding compatibility. In Chapter 27, I discuss
various ways to use VBA to work with files. In Chapter 28, I explain how to use VBA
4799-2 FM.F 6/11/01 1:12 PM Page xiii
xiv
Preface

to manipulate Visual Basic components such as UserForms and modules. Chapter
29 covers the topic of class modules. I finish the part with a useful chapter that
answers many common questions about Excel programming.
Appendixes
Five appendixes round out the book. Appendix A contains useful information about
Excel resources online. Appendix B is a reference guide to all of VBA’s keywords
(statements and functions). I explain VBA error codes in Appendix C, and Appendix
D is a handy ANSI code reference chart. The final appendix describes the files avail-
able on the companion CD-ROM.
About the Companion CD-ROM
The inside back cover of this book contains a CD-ROM that holds many useful
examples that I discuss in the text. When I write about computer-related material,
I emphasize learning by example. I know that I learn more from a well-thought-out
example than from reading a dozen pages in a book. I assume that this is true for
many other people. Consequently, I spent more time developing the examples on
the CD-ROM than I did writing chapters.
The files on the companion CD-ROM are not compressed, so you can access them
directly from the CD.
Refer to Appendix E for a description of each file on the CD-ROM.
All CD-ROM files are read-only files. Therefore, if you open a file from the CD-ROM
and make any changes to it, you need to save it to your hard drive. In addition, if
you copy a file from the CD-ROM to your hard drive, the file retains its read-only
attribute. To change this attribute after copying a file, right-click the filename or
icon and select Properties from the shortcut menu. In the Properties dialog box,
click the General tab and remove the check mark from the Read-only checkbox.
About the Power Utility Pak Offer
Toward the back of the book, you’ll find a coupon that you can redeem for a free
copy of my popular Power Utility Pak software (normally $39.95). PUP is an award-
winning collection of useful Excel utilities and many new worksheet functions. I
developed this package exclusively with VBA.

Note
Cross-
Reference
4799-2 FM.F 6/11/01 1:12 PM Page xiv
xv
Preface
I think you’ll find this product useful in your day-to-day work with Excel, and I urge
you to take advantage of this free offer. You can also purchase the complete VBA
source code for a nominal fee. Studying the code is an excellent way to pick up
some useful programming techniques.
You can take Power Utility Pak for a test drive by installing the 30-day trial version
from the companion CD-ROM.
How to Use This Book
You can use this book any way you please. If you choose to read it cover to cover, be
my guest. But because I’m dealing with intermediate-to-advanced subject matter, the
chapter order is often immaterial. I suspect that most readers will skip around, pick-
ing up useful tidbits here and there. If you’re faced with a challenging task, you might
try the index first to see whether the book specifically addresses your problem.
Reach Out
The publisher and I want your feedback. After you have had a chance to use this
book, please take a moment to visit the Hungry Minds, Inc. Web site to register your
book and give us your comments. (See the “my2cents.hungryminds.com” page at
the back of this book for more details.) Please be honest in your evaluation. If you
thought a particular chapter didn’t tell you enough, let me know. Of course, I would
prefer to receive comments like “This is the best book I’ve ever read,” or “Thanks to
this book, I was promoted and now make $90,000 a year.”
I get at least a dozen questions every day, via e-mail, from people who have read
my books. I appreciate the feedback. Unfortunately, I simply don’t have the time to
reply to questions. Appendix A provides a good list of sources that can answer your
questions.

I also invite you to visit my Web site, which contains lots of Excel-related material.
Despite the massive attempts to make this book completely accurate, a few errors
have probably crept into its pages. My Web site includes a list of any such errors.
The URL is
/>4799-2 FM.F 6/11/01 1:12 PM Page xv
4799-2 FM.F 6/11/01 1:12 PM Page xvi

Contents at a Glance
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Part I: Some Essential Background . . . . . . . . . . . . . . . . . . . . . 1
Chapter 1: Excel 2002: Where It Came From . . . . . . . . . . . . . . . . . . . . . . 3
Chapter 2: Excel in a Nutshell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Chapter 3: Formula Tricks and Techniques . . . . . . . . . . . . . . . . . . . . . . 37
Chapter 4: Understanding Excel’s Files . . . . . . . . . . . . . . . . . . . . . . . . 61
Part II: Excel Application Development . . . . . . . . . . . . . . . . . . 79
Chapter 5: What is a Spreadsheet Application? . . . . . . . . . . . . . . . . . . . . 81
Chapter 6: Essentials of Spreadsheet Application Development . . . . . . . . . . 95
Part III: Understanding Visual Basic for Applications . . . . . . . . . 117
Chapter 7: Introducing Visual Basic for Applications . . . . . . . . . . . . . . . . 119
Chapter 8: VBA Programming Fundamentals . . . . . . . . . . . . . . . . . . . . 177
Chapter 9: Working with VBA Sub Procedures . . . . . . . . . . . . . . . . . . . . 219
Chapter 10: Creating Function Procedures . . . . . . . . . . . . . . . . . . . . . . 257
Chapter 11: VBA Programming Examples and Techniques . . . . . . . . . . . . . 291
Part IV: Working with UserForms . . . . . . . . . . . . . . . . . . . . . 353
Chapter 12: Custom Dialog Box Alternatives . . . . . . . . . . . . . . . . . . . . . 355
Chapter 13: Introducing UserForms . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Chapter 14: UserForm Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Chapter 15: Advanced UserForm Techniques . . . . . . . . . . . . . . . . . . . . 439
Part V: Advanced Programming Techniques . . . . . . . . . . . . . . 475

Chapter 16: Developing Excel Utilities with VBA . . . . . . . . . . . . . . . . . . 477
Chapter 17: Working with Pivot Tables . . . . . . . . . . . . . . . . . . . . . . . . 505
Chapter 18: Working with Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
Chapter 19: Understanding Excel’s Events . . . . . . . . . . . . . . . . . . . . . . 571
Chapter 20: Interacting with Other Applications . . . . . . . . . . . . . . . . . . 603
Chapter 21: Creating and Using Add-Ins . . . . . . . . . . . . . . . . . . . . . . . 625
4799-2 FM.F 6/11/01 1:12 PM Page xviii
xix
Contents
Part VI: Developing Applications . . . . . . . . . . . . . . . . . . . . . 651
Chapter 22: Creating Custom Toolbars . . . . . . . . . . . . . . . . . . . . . . . . 653
Chapter 23: Creating Custom Menus . . . . . . . . . . . . . . . . . . . . . . . . . 685
Chapter 24: Providing Help for Your Applications . . . . . . . . . . . . . . . . . . 719
Chapter 25: Developing User-Oriented Applications . . . . . . . . . . . . . . . . 739
Part VII: Other Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751
Chapter 26: Compatibility Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . 753
Chapter 27: Manipulating Files with VBA . . . . . . . . . . . . . . . . . . . . . . . 767
Chapter 28: Manipulating Visual Basic Components . . . . . . . . . . . . . . . . 789
Chapter 29: Understanding Class Modules . . . . . . . . . . . . . . . . . . . . . . 811
Chapter 30: Frequently Asked Questions about Excel Programming . . . . . . . 825
Appendix A: Excel Resources Online . . . . . . . . . . . . . . . . . . . . . . . . . 861
Appendix B: VBA Statements and Functions Reference . . . . . . . . . . . . . . 867
Appendix C: VBA Error Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 877
Appendix D: ANSI Code Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . 881
Appendix E: What’s on the CD-ROM . . . . . . . . . . . . . . . . . . . . . . . . . . 891
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 907
End-User License Agreement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 943
CD-ROM Installation Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . 946
4799-2 FM.F 6/11/01 1:12 PM Page xix
4799-2 FM.F 6/11/01 1:12 PM Page xx

Contents
Part I: Some Essential Background 1
Chapter 1: Excel 2002: Where It Came From . . . . . . . . . . . . . . . . 3
A Brief History of Spreadsheets . . . . . . . . . . . . . . . . . . . . . . . . . . 3
It all started with VisiCalc . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Lotus 1-2-3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Quattro Pro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Microsoft Excel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Spreadsheets Today . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Why Excel Is Great for Developers . . . . . . . . . . . . . . . . . . . . . . . . 14
Excel’s Role in Microsoft’s Strategy . . . . . . . . . . . . . . . . . . . . . . . 15
Chapter 2: Excel in a Nutshell . . . . . . . . . . . . . . . . . . . . . . . . 17
Thinking in Terms of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Workbooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Worksheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Chart sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
XLM macro sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Excel 5/95 dialog sheets . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Excel’s User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Dialog boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Toolbars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Drag-and-drop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Keyboard shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Customizing the Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Data Entry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Selecting Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Numeric formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Stylistic formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

Formulas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Worksheet databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
External databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
4799-2 FM.F 6/11/01 1:12 PM Page xxi
xxii
Contents
Internet Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Analysis Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Outlines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Automatic subtotals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Scenario management . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Analysis ToolPak . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Pivot tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Auditing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Solver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Add-Ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Chapter 3: Formula Tricks and Techniques . . . . . . . . . . . . . . . . 37
About Formulas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Calculating Formulas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Cell and Range References . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Why use references that aren’t relative? . . . . . . . . . . . . . . . . . 39
About R1C1 notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Referencing other sheets or workbooks . . . . . . . . . . . . . . . . . 41

Using Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Naming cells and ranges . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Applying names to existing references . . . . . . . . . . . . . . . . . . 43
Intersecting names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Naming columns and rows . . . . . . . . . . . . . . . . . . . . . . . . . 45
Scoping names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Naming constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Naming formulas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Naming objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Formula Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Array Formulas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
An array formula example . . . . . . . . . . . . . . . . . . . . . . . . . 50
An array formula calendar . . . . . . . . . . . . . . . . . . . . . . . . . 51
Array formula pros and cons . . . . . . . . . . . . . . . . . . . . . . . . 52
Counting and Summing Techniques . . . . . . . . . . . . . . . . . . . . . . . 52
Using the COUNTIF or SUMIF function . . . . . . . . . . . . . . . . . . 52
Using array formulas to count and sum . . . . . . . . . . . . . . . . . 53
Other counting tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Working with Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Entering dates and times . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Using pre-1900 dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Creating Megaformulas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Chapter 4: Understanding Excel’s Files . . . . . . . . . . . . . . . . . . 61
Starting Excel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Excel’s File Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Spreadsheet File Formats Supported . . . . . . . . . . . . . . . . . . . . . . 64
Lotus 1-2-3 spreadsheet files . . . . . . . . . . . . . . . . . . . . . . . . 64
Quattro Pro spreadsheet files . . . . . . . . . . . . . . . . . . . . . . . 65
4799-2 FM.F 6/11/01 1:12 PM Page xxii
xxiii

Contents
Database file formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Text file formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Other file formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Files Written by Excel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
XLS files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Workspace files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Template files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Toolbar files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Add-in files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Excel and HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
So how does it work? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Adding some complexity . . . . . . . . . . . . . . . . . . . . . . . . . . 71
What about interactivity? . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Excel Settings in the Registry . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
About the Registry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Excel’s settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Part II: Excel Application Development 79
Chapter 5: What Is a Spreadsheet Application? . . . . . . . . . . . . . 81
Spreadsheet Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
The Developer and the End User . . . . . . . . . . . . . . . . . . . . . . . . . 83
Who are developers? What do they do? . . . . . . . . . . . . . . . . . 83
Classifying spreadsheet users . . . . . . . . . . . . . . . . . . . . . . . 84
The audience for spreadsheet applications . . . . . . . . . . . . . . . 85
Why people use spreadsheets . . . . . . . . . . . . . . . . . . . . . . . 86
Solving Problems with a Spreadsheet . . . . . . . . . . . . . . . . . . . . . . 88
Basic Spreadsheet Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Quick-and-dirty spreadsheets . . . . . . . . . . . . . . . . . . . . . . . 90
For-your-eyes-only spreadsheets . . . . . . . . . . . . . . . . . . . . . 90
Single-user applications . . . . . . . . . . . . . . . . . . . . . . . . . . . 90

Spaghetti applications . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Utility applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Add-ins that contain worksheet functions . . . . . . . . . . . . . . . . 92
Single-block budgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
What-if models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Data storage and access spreadsheets . . . . . . . . . . . . . . . . . . 93
Database front ends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Turnkey applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Chapter 6: Essentials of Spreadsheet Application Development . . . 95
Determining User Needs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Planning an Application That Meets User Needs . . . . . . . . . . . . . . . . 97
4799-2 FM.F 6/11/01 1:12 PM Page xxiii

×