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 [...]... 14 3 14 4 14 5 14 9 15 0 15 2 15 2 15 3 15 3 15 4 15 4 15 5 15 7 15 7 15 8 15 8 15 9 16 0 16 1 16 3 16 3 16 3 16 5 16 6 16 7 16 9 17 0 17 0 17 1 Chapter 8: VBA Programming Fundamentals 17 7 VBA Language Elements: An Overview Comments Variables, Data Types, and Constants Defining data types Declaring variables Scoping variables Working with constants... of Excel Language issues System speed Video modes Directory structure Part III: Understanding Visual Basic for Applications 10 0 10 0 10 1 10 3 10 5 10 6 10 7 10 7 10 7 10 9 11 0 11 1 11 2 11 3 11 3 11 4 11 4 11 4 11 5 11 5... 11 9 12 0 12 0 12 0 12 1 12 2 12 2 12 5 12 5 12 6 12 8 12 9 12 9 12 9 13 0 13 0 13 1 13 2 13 8 13 8 14 1 14 2 14 3 4799-2 FM.F 6 /11 / 01 1 :12 PM Page xxv Contents The Macro Recorder What is recorded Relative or absolute? Recording options... 17 7 17 8 18 1 18 2 18 4 18 6 19 0 19 1 19 2 19 3 19 5 19 5 19 6 19 6 19 8 19 8 xxv 4799-2 FM.F xxvi 6 /11 / 01 1 :12 PM Page xxvi Contents Manipulating Objects and Collections With- End With constructs For Each-Next constructs Controlling Execution GoTo statements If-Then constructs... 307 308 310 310 311 312 314 314 315 315 316 317 317 318 318 320 3 21 323 325 325 325 326 326 326 327 328 329 329 330 3 31 332 332 333 335 336 337 338 339 339 340 342 344 344 345 346 347 349 4799-2 FM.F 6 /11 / 01 1 :12 PM Page xxix Contents Part IV: Working with UserForms 353 Chapter 12 : Custom Dialog Box Alternatives 355 Before You Create... 411 411 412 413 414 416 417 418 420 4 21 4 21 425 426 426 427 428 430 432 434 436 4799-2 FM.F 6 /11 / 01 1 :12 PM Page xxxi Contents Chapter 15 : Advanced UserForm Techniques 439 Displaying a Progress Indicator Creating a standalone progress indicator Showing progress using a MultiPage control Showing progress without using a MultiPage control... Applications 11 7 Chapter 7: Introducing Visual Basic for Applications Chapter 8: VBA Programming Fundamentals Chapter 9: Working with VBA Sub Procedures Chapter 10 : Creating Function Procedures Chapter 11 : VBA Programming Examples and Techniques 11 9 17 7 219 257 2 91 Part IV: Working with UserForms ... 8 61 867 877 8 81 8 91 Index 907 End-User License Agreement 943 CD-ROM Installation Instructions 946 xix 4799-2 FM.F 6 /11 / 01 1 :12 PM Page xx 4799-2 FM.F 6 /11 / 01 1 :12 PM Page xxi Contents Part I: Some Essential Background 1 Chapter 1: Excel 2002: Where It...4799-2 FM.F 6 /11 / 01 1 :12 PM Page ix 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... Working with Pivot Tables Chapter 18 : Working with Charts Chapter 19 : Understanding Excel s Events Chapter 20: Interacting with Other Applications Chapter 21: Creating and Using Add-Ins 477 505 5 21 5 71 603 625 4799-2 FM.F 6 /11 / 01 1 :12 PM Page xix Contents Part VI: Developing . 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. 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. 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