Microsoft Office Excel 2003 Programming Inside Out
www.it-ebooks.info
PUBLISHED BY
Microsoft Press
A Division of Microsoft Corporation
One Microsoft Way
Redmond, Washington 98052-6399
Copyright © 2004 by Microsoft Corporation
All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by
any means without the written permission of the publisher.
Library of Congress Cataloging-in-Publication Data
Frye, Curtis, 1968Microsoft Office Excel 2003 Programming Inside Out / Curtis Frye, Wayne S. Freeze, Felicia K.
Buckingham.
p. cm.
Includes index.
ISBN 0-7356-1985-9
1. Microsoft Excel (Computer file) 2. Business--Computer programs. 3. Electronic
spreadsheets. I. Title.
HF5548.4.M523F792 2003
005.54--dc22
2003064850
Printed and bound in the United States of America.
1 2 3 4 5 6 7 8 9
QWT
8 7 6 5 4 3
Distributed in Canada by H.B. Fenn and Company Ltd.
A CIP catalogue record for this book is available from the British Library.
Microsoft Press books are available through booksellers and distributors worldwide. For further information
about international editions, contact your local Microsoft Corporation office or contact Microsoft Press
International directly at fax (425) 936-7329. Visit our Web site at www.microsoft.com/mspress. Send comments
to mspinput@microsoft.com.
ActiveX, BizTalk, DirectX, FrontPage, Hotmail, IntelliSense, MapPoint, Microsoft, Microsoft Press,
MSDN, MSN, Outlook, PivotChart, PivotTable, PowerPoint, SharePoint, Tahoma, Visio, Visual Basic,
Visual C++, Visual C#, Visual Studio, Windows, the Windows logo, Windows NT, and Windows
Server are either registered trademarks or trademarks of Microsoft Corporation in the United States
and/or other countries. Other product and company names mentioned herein may be the trademarks of
their respective owners.
The example companies, organizations, products, domain names, e-mail addresses, logos, people, places,
and events depicted herein are fictitious. No association with any real company, organization, product,
domain name, e-mail address, logo, person, place, or event is intended or should be inferred.
This book expresses the author’s views and opinions. The information contained in this book is provided
without any express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its
resellers or distributors will be held liable for any damages caused or alleged to be caused either directly
or indirectly by this book.
Acquisitions Editor: Juliana Aldous
Project Editor: Dick Brown
Technical Reviewer: Mark Dodge
Indexer: Richard Shrout
Body Part No. X10-09355
www.it-ebooks.info
To our families
www.it-ebooks.info
www.it-ebooks.info
Contents at a Glance
Part 4
Part 1
Preliminaries
1€
Advanced VBA
191€
Chapter 1
What’s New in Excel 2003 . . . . . . . 3
Chapter 9
Manipulating Data with VBA. . . . 193
Chapter 2
Exploring Excel . . . . . . . . . . . . . . 13
Chapter 10
Formatting Excel Objects . . . . . . 219
Part 2
Chapter 11
Creating Add-Ins and
COM Add-Ins . . . . . . . . . . . . . . . 239
Visual Basic €
for Applications
23€
Chapter 3
Exploring Visual Basic
for Applications . . . . . . . . . . . . . . 25
Chapter 4
VBA Programming Starter Kit . . . . 41
Chapter 5
Creating Sub and
Function Procedures. . . . . . . . . . . 85
Chapter 13
Manipulating Files . . . . . . . . . . . 281
Chapter 14
Developing Class Modules . . . . . 299
Part 5
Manipulating €
Excel Objects
Part 3
The Excel €
Object Model
Chapter 12
Understanding and
Using Events . . . . . . . . . . . . . . . 265
105€
319€
Chapter 15
Charts . . . . . . . . . . . . . . . . . . . . 321
Chapter 6
The Application Object . . . . . . . . 107
Chapter 7
Workbooks and Worksheets . . . . 129
Chapter 8
Ranges and Cells . . . . . . . . . . . . 161
Chapter 16
PivotTables and PivotCharts. . . . 341
Chapter 17
Command Bars . . . . . . . . . . . . . 365
Chapter 18
Customizing Dialog Boxes . . . . . 383
v
www.it-ebooks.info
Contents At A Glance
Chapter 19
Creating User Forms . . . . . . . . . . 395
Chapter 20
Creating Advanced
User Forms. . . . . . . . . . . . . . . . . 419
Chapter 23
Introducing ADO . . . . . . . . . . . . 481
Chapter 24
Excel Query Program . . . . . . . . . 497
Part 6
Excel and the €
Outside World: €
Collaborating €
Made Easy
Chapter 22
Excel and the
Structured Query Language . . . . 467
Chapter 25
Excel and the Web . . . . . . . . . . . 519
443€
Chapter 21
Excel and Other Office
Applications . . . . . . . . . . . . . . . . 445
Chapter 26
Excel and the Extensible
Markup Language (XML) . . . . . . 539
vi
www.it-ebooks.info
Table of Contents
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
We’d Like to Hear from You . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
About the CD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
What’s o n the CD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Using the CD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
System Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv
Support Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv
Conventions and Features Used in this Book . . . . . . . . . . . . . . . . . . . . . . . . xxv
Text Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Design Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Part 1
Preliminaries
Chapter 1
What’s New in Excel 2003
3
Using Improved Statistical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Creating Smart Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Creating Document Workspaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Implementing Information Rights Management . . . . . . . . . . . . . . . . . . . . . . 7
Comparing Workbooks Side By Side. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Getting Information with the Research Task Pane . . . . . . . . . . . . . . . . . . . . 8
Using Extended XML Capabilities. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Creating Excel Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Mapping Excel Data to XML Schemas . . . . . . . . . . . . . . . . . . . . . . . 10
Publishing Spreadsheets with Web Components . . . . . . . . . . . . . . . . 11
Chapter 2
Exploring Excel
13
Workbooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Worksheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Cells and Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Excel Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Formulas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
14
15
18
21
vii
www.it-ebooks.info
Table of Contents
Part 2
Visual Basic for Applications
Chapter 3
Exploring Visual Basic for Applications
25
An Overview of Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . .
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Working with Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Recording and Viewing Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Running a Macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Debugging a Macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Implementing Macro Security and Digital Signatures . . . . . . . . . . . . .
25
26
27
27
28
29
29
31
34
36
Chapter 4
VBA Programming Starter Kit
41
Introducing the Visual Basic Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Opening the Visual Basic Editor. . . . . . . . . . . . . . . . . . . . . . . . . . . .
Recognizing Parts of the Visual Basic Editor . . . . . . . . . . . . . . . . . . .
Customizing the Visual Basic Editor . . . . . . . . . . . . . . . . . . . . . . . . .
Managing Code Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handling Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adding Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Notes on Programming Style and Code Readability . . . . . . . . . . . . . .
Developing Projects with the Project Explorer . . . . . . . . . . . . . . . . . . . . . .
Creating VBA Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Deleting VBA Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Copying Modules Between Projects . . . . . . . . . . . . . . . . . . . . . . . . .
Declaring Variables, Constants, and Data Types . . . . . . . . . . . . . . . . . . . .
Declaring Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Defining Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Defining Constants. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handling Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handling Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handling Variants and Data Type Conversions. . . . . . . . . . . . . . . . . .
Understanding Variable Scope and Lifetimes . . . . . . . . . . . . . . . . . .
Assigning Values to Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Working with Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Multidimensional Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Dynamic Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
viii
www.it-ebooks.info
41
41
41
49
53
53
53
54
55
55
56
57
57
58
58
59
59
60
61
62
64
64
65
65
66
Table of Contents
Writing to the Screen and Accepting Input . . . . . . . . . . . . . . . . . . . . . . . . 67
Creating a Message Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Creating an Input Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Defining Object Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Creating Custom Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
With...End With Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Controlling Program Flow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Selection Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
GoTo Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .82
Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Chapter 5
Creating Sub and Function Procedures
85
Defining Sub Procedures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Defining the Scope of a Sub Procedure . . . . . . . . . . . . . . . . . . . . . . 90
Running a Sub Procedure from Within Another Procedure . . . . . . . . . 93
Retaining Values Between Procedure Calls . . . . . . . . . . . . . . . . . . . . 98
Defining Function Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Creating a Function Procedure. . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Running Function Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Passing Arguments to Procedures . . . . . . . . . . . . . . . . . . . . . . . . . 101
Passing Named Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Organizing for Success . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Part 3
The Excel Object Model
Chapter 6
The Application Object
107
Introducing the Application Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Chapter 7
Workbooks and Worksheets
129
The Workbooks Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating New Workbooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Opening Workbooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Workbook Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Workbook Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Printing and Previewing Workbooks . . . . . . . . . . . . . . . . . . . . . . . .
129
129
130
138
145
147
ix
www.it-ebooks.info
Table of Contents
The Sheets and Worksheets Collections . . . . . . . . . . . . . . . . . . . . . . . . .
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Worksheet Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
149
149
150
156
Chapter 8
Ranges and Cells
161
Basic Range Manipulations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Finding the Active Range. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Selecting a Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Referring to Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Referencing Ranges on the Active Worksheet . . . . . . . . . . . . . . . . .
Referencing Ranges on an Inactive Worksheet . . . . . . . . . . . . . . . .
Referencing Cells in a Range. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Referencing Cells Using the Offset Property . . . . . . . . . . . . . . . . . .
Defining a Range Using the Cells Property . . . . . . . . . . . . . . . . . . .
Referencing Columns and Rows . . . . . . . . . . . . . . . . . . . . . . . . . .
Referencing Non-Contiguous Groups of Cells . . . . . . . . . . . . . . . . .
Manipulating Groups of Cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Resizing Cells Programmatically . . . . . . . . . . . . . . . . . . . . . . . . . .
Joining Two Ranges Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Detecting Empty Cells. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Named Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Defining a Named Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Changing Notation Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reserved Range Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Copying Data Between Ranges and Arrays . . . . . . . . . . . . . . . . . . .
Getting Data Entry Right the First Time . . . . . . . . . . . . . . . . . . . . . . . . .
161
161
162
166
166
167
167
167
172
172
172
172
172
173
176
177
177
179
182
185
187
Part 4
Advanced VBA
Chapter 9
Manipulating Data with VBA
193
Manipulating Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Determining if the Value in a Cell Is Text . . . . . . . . . . . . . . . . . . . .
Preparing String Data for Processing . . . . . . . . . . . . . . . . . . . . . . .
Determining the Number of Characters in a String . . . . . . . . . . . . .
Concatenating Text from Two or More Cells or Variables . . . . . . . . .
Returning the First or Last Several Characters from a String . . . . . .
Returning Characters from Arbitrary Positions in a String. . . . . . . . .
Finding a String Within Another String . . . . . . . . . . . . . . . . . . . . . .
Manipulating Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Performing Summary Calculations . . . . . . . . . . . . . . . . . . . . . . . . .
Performing Financial Calculations . . . . . . . . . . . . . . . . . . . . . . . . .
x
www.it-ebooks.info
193
193
195
197
198
200
202
202
206
206
208
Table of Contents
Manipulating Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Time and Date Serial Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Date and Time Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Chapter 10
Formatting Excel Objects
219
Using Excel Color Constants and the Color Palette . . . . . . . . . . . . . . . . .
Manipulating the Current Excel Color Palette . . . . . . . . . . . . . . . . .
Using the Excel Color Palette on the Web . . . . . . . . . . . . . . . . . . . .
Formatting Worksheet Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Formatting Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Formatting Cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Formatting Borders. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
219
223
226
230
231
233
236
Chapter 11
Creating Add-Ins and COM Add-Ins
239
Introducing Add-Ins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Add-Ins dialog box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Installing an Add-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Unloading an Add-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Excel Add-Ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating an Add-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Saving the Add-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Installing the Add-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the AddIns Collection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
AddIns Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
AddIn Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Automation and COM Add-Ins . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the IDTExtensibility2 Interface. . . . . . . . . . . . . . . . . . . . . . . .
Registry Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Building an Automation Add-In with Visual Basic 6 . . . . . . . . . . . . . . . . .
Designing the Add-In. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Registry Entries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Building a COM Add-In with Visual Basic .NET. . . . . . . . . . . . . . . . . . . . .
Running the Shared Add-In Wizard . . . . . . . . . . . . . . . . . . . . . . . . .
Modifying the Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Installing the Add-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
239
240
241
241
241
241
242
243
244
244
245
247
248
250
250
250
256
256
257
259
261
Chapter 12
Understanding and Using Events
265
Enabling and Disabling Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Workbook Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Open Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Activate Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
SheetActivate Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
266
267
269
270
270
xi
www.it-ebooks.info
Table of Contents
NewSheet Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
BeforeSave Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Deactivate Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
BeforePrint Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
BeforeClose Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Worksheet Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Change Event. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
SelectionChange Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
BeforeRightClick Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Application Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Turning on Application Event Monitoring . . . . . . . . . . . . . . . . . . . . .
Detecting When a Workbook Is Opened . . . . . . . . . . . . . . . . . . . . .
270
271
271
271
272
273
275
276
276
276
278
279
Chapter 13
Manipulating Files
281
Locating External Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Returning All Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Limiting the File Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Finding Files with the FileDialog Dialog Box . . . . . . . . . . . . . . . . . .
Writing to an External File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reading from an External File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Searching a File for a Value. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
281
282
285
291
295
296
298
Chapter 14
Developing Class Modules
299
What Is an Object? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
What Is a Class? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
What Are Properties? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
What Are Methods? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
What Are Events?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Introducing Class Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Accessing Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Declaring Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Objects and Nothing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Objects with Multiple Object Variables . . . . . . . . . . . . . . . . . . . . . .
Properties, Methods, and Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Public vs. Private Properties, Methods, and Events . . . . . . . . . . . . .
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Building a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a Class Module. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Defining Simple Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Defining Property Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Defining Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xii
www.it-ebooks.info
299
299
299
300
300
300
301
302
302
303
303
303
304
304
304
305
305
305
306
309
Table of Contents
Defining Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Defining Private Variables, Subroutines, and Functions . . . . . . . . . .
Special Events for Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Resolving References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practical Class Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Simple Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Extending a Simple Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Collection Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Class with Business Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
310
310
311
311
312
312
313
313
317
Part 5
Manipulating Excel Objects
Chapter 15
Charts
321
Introducing Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Embedded Charts or Chart Sheets . . . . . . . . . . . . . . . . . .
Defining the Chart Object Model . . . . . . . . . . . . . . . . . . . . . . . . . .
Manipulating Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Activating a Chart. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Deactivating a Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Modifying a Chart’s Data Series . . . . . . . . . . . . . . . . . . . . . . . . . .
Modifying a Chart to Use Data from Arrays . . . . . . . . . . . . . . . . . . .
Defining a Chart’s Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Formatting a Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Modifying All Charts in a Workbook . . . . . . . . . . . . . . . . . . . . . . . .
Printing Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Final Thoughts on Programming Charts . . . . . . . . . . . . . . . . . . . . . . . . .
321
322
326
327
327
330
331
333
335
336
338
339
339
Chapter 16
PivotTables and PivotCharts
341
PivotTables and PivotCharts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Introducing PivotTables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a PivotTable with the PivotTable Wizard . . . . . . . . . . . . . . .
Introducing PivotCharts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a PivotChart with the PivotTable Wizard . . . . . . . . . . . . . . .
Online Analytical Processing (OLAP) Issues . . . . . . . . . . . . . . . . . .
PivotTable Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
PivotTables Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
PivotTable Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
PivotCaches Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
PivotCache Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
PivotField Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
PivotItem Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
341
341
343
346
346
347
348
349
349
351
352
353
356
xiii
www.it-ebooks.info
Table of Contents
Programming PivotTables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a PivotTable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a PivotChart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Manipulating PivotTables Programmatically. . . . . . . . . . . . . . . . . . . . . . .
Pivoting a PivotTable Programmatically . . . . . . . . . . . . . . . . . . . . . .
Resetting a PivotTable to Its Original Position . . . . . . . . . . . . . . . . .
Recording and Restoring Arbitrary PivotTable Positions . . . . . . . . . .
356
357
358
359
359
361
361
Chapter 17
Command Bars
365
Identifying Parts of the Menu System. . . . . . . . . . . . . . . . . . . . . . . . . . .
CommandBars Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
CommandBar Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Listing CommandBar Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adding a Floating Command Bar . . . . . . . . . . . . . . . . . . . . . . . . . .
Deleting a Command Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Command Bar Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
CommandBarControls Collection . . . . . . . . . . . . . . . . . . . . . . . . . .
CommandBarControl Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Button Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Toolbars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Combo Box Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using a Combo Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Pop-Up Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Displaying a Pop-Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adding Items to an Existing Menu . . . . . . . . . . . . . . . . . . . . . . . . .
365
366
367
369
370
371
371
371
373
375
377
378
379
380
380
382
Chapter 18
Customizing Dialog Boxes
383
Displaying Existing Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Modifying Existing Dialog Boxes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exploring the Dialogs Collection . . . . . . . . . . . . . . . . . . . . . . . . . . .
Passing Arguments to Existing Dialog Boxes. . . . . . . . . . . . . . . . . .
Planning with Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
384
388
389
393
394
Chapter 19
Creating User Forms
395
Creating a UserForm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adding a UserForm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Designing a UserForm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Modifying a UserForm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Properties of a UserForm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Displaying a UserForm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xiv
www.it-ebooks.info
395
395
396
397
398
399
Table of Contents
UserForm Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Programming Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Common Properties, Methods, and Events. . . . . . . . . . . . . . . . . . .
The Label Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The CommandButton Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The TextBox Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The CheckBox Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The ToggleButton Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The SpinButton Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Frame Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The OptionButton Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Image Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The ScrollBar Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The ListBox Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The ComboBox Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The RefEdit Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The TabStrip Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The MultiPage Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
401
401
402
404
404
405
406
407
407
408
409
410
411
412
414
415
415
417
Chapter 20
Creating Advanced User Forms
419
Capturing Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Form Application Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Designing a Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Displaying Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Navigating the Worksheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Editing Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adding Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Validating Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Displaying the User Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Building a Multi-Step Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Wizard Application Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handling Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Building the UserForm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Navigating the Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Collecting Options for the Wizard . . . . . . . . . . . . . . . . . . . . . . . . . .
Summarizing the Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Running the Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
419
419
420
422
425
427
428
429
430
431
431
433
434
435
438
440
441
xv
www.it-ebooks.info
Table of Contents
Part 6
Excel and the Outside World: Collaborating Made Easy
Chapter 21
Excel and Other Office Applications
445
Starting Another Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Activating Another Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Late Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Early Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Interacting with Other Office Applications . . . . . . . . . . . . . . . . . . . . . . . .
Opening a Document in Word . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Accessing an Active Word Document . . . . . . . . . . . . . . . . . . . . . . .
Creating a New Word Document . . . . . . . . . . . . . . . . . . . . . . . . . .
Controlling Excel from Other Office Applications . . . . . . . . . . . . . . .
Working with Multiple Applications to Get the Job Done. . . . . . . . . . . . . .
445
449
449
450
452
455
457
460
461
462
465
Chapter 22
Excel and the Structured Query Language
467
Comparing Spreadsheets and Databases. . . . . . . . . . . . . . . . . . . . . . . .
Fundamental Database Concepts . . . . . . . . . . . . . . . . . . . . . . . . .
Database Keys. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Accessing Databases from Excel . . . . . . . . . . . . . . . . . . . . . . . . . .
Manipulating Databases with SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Select Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Simple Select Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Retrieving Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Sorting Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Multiple Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Insert Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Insert Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Update Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Delete Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
467
467
469
470
471
471
472
473
475
476
477
478
478
479
479
Chapter 23
Introducing ADO
481
The ADO Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the ADO Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Connection Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Key Properties and Methods of the Connection Object. . . . . . . . . . .
Connecting to Different Database Management Systems . . . . . . . .
Using the Errors Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Error Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xvi
www.it-ebooks.info
482
483
483
484
485
486
487
Table of Contents
The Command Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Key Properties and Methods of the Command Object . . . . . . . . . . .
Using the Parameters Collection . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Parameter Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Recordset Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Key Properties and Methods of the Recordset Object . . . . . . . . . . .
Using the Fields Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Field Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
487
488
489
490
491
491
494
494
Chapter 24
Excel Query Program
497
Excel Query Program Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . .
Setting Up the Project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Initializing the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Ending the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Connecting to a Database. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Initializing the DBInfo UserForm . . . . . . . . . . . . . . . . . . . . . . . . . . .
Changing Database Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Selecting Windows Authentication . . . . . . . . . . . . . . . . . . . . . . . . .
Saving the Database Info . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Editing a Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Executing a Database Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Getting the Information to Run the Query . . . . . . . . . . . . . . . . . . . .
Building a Connection String . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Getting the Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Running a Query. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Copying Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Excel Query Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Configure the Connection Information . . . . . . . . . . . . . . . . . . . . . .
Enter a Query. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Run the Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
497
498
499
500
501
502
503
504
504
505
508
508
509
512
512
514
515
516
516
517
Chapter 25
Excel and the Web
519
HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Saving a Worksheet as a Web Page . . . . . . . . . . . . . . . . . . . . . . . .
Publishing a Worksheet to the Web . . . . . . . . . . . . . . . . . . . . . . . .
Making Web-Based Worksheets Interactive. . . . . . . . . . . . . . . . . . .
Using the Internet as a Data Source . . . . . . . . . . . . . . . . . . . . . . . . . . .
Opening Web Pages as Workbooks . . . . . . . . . . . . . . . . . . . . . . . .
Using Web Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Parsing Web Pages for Specific Information . . . . . . . . . . . . . . . . . .
Using the Internet to Publish Results . . . . . . . . . . . . . . . . . . . . . . . . . . .
Setting Up a Web Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Saving Worksheets as Web Pages . . . . . . . . . . . . . . . . . . . . . . . . .
520
520
523
525
527
528
529
532
533
533
534
xvii
www.it-ebooks.info
Table of Contents
Adding Interactivity with the Web Components . . . . . . . . . . . . . . . . 534
Communicating with a Web Server. . . . . . . . . . . . . . . . . . . . . . . . . 536
Using Internet Solutions with Excel . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
Chapter 26
Excel and the Extensible Markup Language (XML)
539
Introducing Data Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Data Lists Programmatically. . . . . . . . . . . . . . . . . . . . . . .
Creating XML Schemas. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating XML Data Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adding XML to a Workbook Manually . . . . . . . . . . . . . . . . . . . . . . . . . . .
Importing XML Data Manually . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adding XML to a Worksheet Programmatically. . . . . . . . . . . . . . . . . . . . .
Mapping a Schema to a Worksheet Programmatically . . . . . . . . . . .
Mapping Schema Elements to Cells Using XPath . . . . . . . . . . . . . .
xviii
www.it-ebooks.info
539
541
546
549
550
552
553
553
557
Acknowledgments
From Curtis Frye:
First and foremost I’d like to thank my co-authors, Wayne Freeze and Felicia Buckingham,
for their knowledge and skill at presenting a series of complex topics so effectively. Westley
Annis pitched in on several chapters as well, for which I am grateful.
I value my relationship with Microsoft Press for many reasons, but right up there on the
list is the terrific working relationship I have with everyone there. I’m happy to say our
relationship survived intact despite many changes in all of our lives that transpired during
this project. Juliana Aldous Atkinson invited me to submit an outline and shepherded the
proposal through the review process. Sandra Haynes was the first project editor, and Dick
Brown took over when Sandra assumed different duties within Press. Wendy Zucker
stepped in to help during the revision process when Dick went on vacation for a week. The
phrase “team effort” doesn’t begin to describe the sort of support they provided.
I was also very fortunate to have Mark Dodge, co-author of Microsoft Office Excel 2003
Inside Out, as my technical editor. His knowledge of both the series and Excel gave him the
perspective to make many terrific recommendations. Lisa Pawlewicz, ably assisted by
Jaime Odell, performed what was probably one of the more involved copy edits that has
passed over their desks in a while. Page proofs were handled by Sandi Resnick at Microsoft
and the nSight team of Joe Armstrong, Steve Boudreault, Catherine Cooker, Beth Lew,
Katie O’Connell, Dan Shaw, Asa Tomash, and Melissa von Tschudi-Sutton. Barbara Levy
and Carl Diltz turned my Word files into the beautiful pages you see before you, William
Teel got the graphics ready for production, Joel Panchot created the original art, and
Richard Shrout created a thorough index. Tess McMillan heads the team that created the
Companion CD.
Finally, I’d like to thank my agent, Neil Salkind of StudioB, for introducing me to
Microsoft back in December of 2000. He was concerned about recommending a relatively
new author to such an important publisher, but I’d say things have turned out pretty well.
From Wayne Freeze:
Curt: thanks for putting up with me while writing this book. I valued your patience, especially when I was running behind schedule as usual. Laura: thank you for making my first
year at StudioB a very worthwhile experience. Steve: thank you for the opportunities and
challenges you tossed my way this year and I have even higher expectations for the next.
Christopher and Samantha: thanks for tolerating the long hours I’ve spent working on this
book alongside my other projects. Jill: thank you for being you—I love you.
From Felicia Buckingham:
Thanks to everyone from Microsoft Press for their work on the project, and thanks to Curt
for the opportunity to co-author my first book.
xix
www.it-ebooks.info
www.it-ebooks.info
We’d Like to Hear from You
Our goal at Microsoft Press is to create books that help you find the information you need to
get the most out of your software.
The Inside Out series was created with you in mind. As part of our ongoing effort to ensure
that we’re creating the books that meet your learning needs, we’d like to hear from you. Let us
know what you think. Tell us what you like about this book and what we can do to make it
better. When you write, please include the title and author of this book in your e-mail mes€
sage, as well as your name and contact information. We look forward to hearing from you!
How to Reach Us
E-Mail:
Mail:
nsideout@microsoft.com
Inside Out Series Editor
Microsoft Press
One Microsoft Way
Redmond, WA 98052
Note: Unfortunately, we can’t provide support for any software problems you might experience.
Please go to http://support.microsoft.com for help with any software issues.
xxi
www.it-ebooks.info
www.it-ebooks.info
About the CD
The Companion CD that ships with this book contains many tools and resources to help you
get the most out of your Inside Out book.
What’s on the CD
Your Inside Out CD includes the following:
●
Complete eBook.
In this section, you’ll find an electronic version of Microsoft Office
Excel 2003 Programming Inside Out. The eBook is in PDF format.
●
Computer Dictionary, Fifth Edition eBook.
Here you’ll find the full electronic
version of the Microsoft Computer Dictionary, Fifth Edition. Suitable for home and
office, the dictionary contains more than 10,000 entries.
●
Insider Extras.
This section includes the sample files used in the book.
●
Microsoft Resources.
In this section, you’ll find information about additional
resources from Microsoft that will help you get the most out of Microsoft Office
Excel 2003 and other business software from Microsoft.
●
Extending Excel.
In this section, you’ll find great information about third-party
utilities and tools you use to further enhance your experience with Microsoft Office
Excel 2003.
The Companion CD provides detailed information about the files on this CD, and links to
Microsoft and third-party sites on the Internet. All the files on this CD are designed to be
accessed through Microsoft Internet Explorer (version 5.01 or later).
Note
The links to third-party sites are not under the control of Microsoft Corporation, and
Microsoft is therefore not responsible for their content, nor should their inclusion on this
CD be construed as an endorsement of the product or the site. Software provided on this
CD is in English language only and may be incompatible with non-English language operating systems and software.
Using the CD
To use this Companion CD, insert it into your CD-ROM drive. If AutoRun is not enabled on
your computer, click on Index.htm in the WebSite folder in the root of the CD.
xxiii
www.it-ebooks.info
About the CD
System Requirements
Following are the minimum system requirements necessary to run the CD:
●
Microsoft Windows XP or later or Windows 2000 Professional with Service Pack 3 or
later.
●
266-MHz or higher Pentium-compatible CPU
●
64 megabytes (MB) RAM
●
8X CD-ROM drive or faster
●
Microsoft Windows–compatible sound card and speakers
●
Microsoft Internet Explorer 5.01 or later
●
Microsoft Mouse or compatible pointing device
Note System requirements may be higher for the add-ins available via links on the CD.
Individual add-in system requirements are specified at the sites listed. An Internet connection is necessary to access some of the hyperlinks. Connect time charges may apply.
Support Information
Every effort has been made to ensure the accuracy of the book and the contents of this
Companion CD. For feedback on the book content or this Companion CD, please contact us
by using any of the addresses listed in the “We’d Like to Hear From You” section.
Microsoft Press provides corrections for books through the World Wide Web at
http://www.microsoft.com/learning/support/. To connect directly to the Microsoft Press
Knowledge Base and enter a query regarding a question or issue that you may have, go to
http://www.microsoft.com/learning/support/search.asp.
For support information regarding Windows XP, you can connect to Microsoft Technical
Support on the Web at http://support.microsoft.
PUBLISHED BY
Microsoft Press
A Division of Microsoft Corporation
One Microsoft Way
Redmond, Washington 98052-6399
Copyright © 2004 by Microsoft Corporation
All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by
any means without the written permission of the publisher.
Library of Congress Cataloging-in-Publication Data
Frye, Curtis, 1968Microsoft Office Excel 2003 Programming Inside Out / Curtis Frye, Wayne S. Freeze, Felicia K.
Buckingham.
p. cm.
Includes index.
ISBN 0-7356-1985-9
1. Microsoft Excel (Computer file) 2. Business--Computer programs. 3. Electronic
spreadsheets. I. Title.
HF5548.4.M523F792 2003
005.54--dc22
2003064850
Printed and bound in the United States of America.
1 2 3 4 5 6 7 8 9
QWT
8 7 6 5 4 3
Distributed in Canada by H.B. Fenn and Company Ltd.
A CIP catalogue record for this book is available from the British Library.
Microsoft Press books are available through booksellers and distributors worldwide. For further information
about international editions, contact your local Microsoft Corporation office or contact Microsoft Press
International directly at fax (425) 936-7329. Visit our Web site at www.microsoft.com/mspress. Send comments
to mspinput@microsoft.com.
ActiveX, BizTalk, DirectX, FrontPage, Hotmail, IntelliSense, MapPoint, Microsoft, Microsoft Press,
MSDN, MSN, Outlook, PivotChart, PivotTable, PowerPoint, SharePoint, Tahoma, Visio, Visual Basic,
Visual C++, Visual C#, Visual Studio, Windows, the Windows logo, Windows NT, and Windows
Server are either registered trademarks or trademarks of Microsoft Corporation in the United States
and/or other countries. Other product and company names mentioned herein may be the trademarks of
their respective owners.
The example companies, organizations, products, domain names, e-mail addresses, logos, people, places,
and events depicted herein are fictitious. No association with any real company, organization, product,
domain name, e-mail address, logo, person, place, or event is intended or should be inferred.
This book expresses the author’s views and opinions. The information contained in this book is provided
without any express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its
resellers or distributors will be held liable for any damages caused or alleged to be caused either directly
or indirectly by this book.
Acquisitions Editor: Juliana Aldous
Project Editor: Dick Brown
Technical Reviewer: Mark Dodge
Indexer: Richard Shrout
Body Part No. X10-09355
www.it-ebooks.info
To our families
www.it-ebooks.info
www.it-ebooks.info
Contents at a Glance
Part 4
Part 1
Preliminaries
1€
Advanced VBA
191€
Chapter 1
What’s New in Excel 2003 . . . . . . . 3
Chapter 9
Manipulating Data with VBA. . . . 193
Chapter 2
Exploring Excel . . . . . . . . . . . . . . 13
Chapter 10
Formatting Excel Objects . . . . . . 219
Part 2
Chapter 11
Creating Add-Ins and
COM Add-Ins . . . . . . . . . . . . . . . 239
Visual Basic €
for Applications
23€
Chapter 3
Exploring Visual Basic
for Applications . . . . . . . . . . . . . . 25
Chapter 4
VBA Programming Starter Kit . . . . 41
Chapter 5
Creating Sub and
Function Procedures. . . . . . . . . . . 85
Chapter 13
Manipulating Files . . . . . . . . . . . 281
Chapter 14
Developing Class Modules . . . . . 299
Part 5
Manipulating €
Excel Objects
Part 3
The Excel €
Object Model
Chapter 12
Understanding and
Using Events . . . . . . . . . . . . . . . 265
105€
319€
Chapter 15
Charts . . . . . . . . . . . . . . . . . . . . 321
Chapter 6
The Application Object . . . . . . . . 107
Chapter 7
Workbooks and Worksheets . . . . 129
Chapter 8
Ranges and Cells . . . . . . . . . . . . 161
Chapter 16
PivotTables and PivotCharts. . . . 341
Chapter 17
Command Bars . . . . . . . . . . . . . 365
Chapter 18
Customizing Dialog Boxes . . . . . 383
v
www.it-ebooks.info
Contents At A Glance
Chapter 19
Creating User Forms . . . . . . . . . . 395
Chapter 20
Creating Advanced
User Forms. . . . . . . . . . . . . . . . . 419
Chapter 23
Introducing ADO . . . . . . . . . . . . 481
Chapter 24
Excel Query Program . . . . . . . . . 497
Part 6
Excel and the €
Outside World: €
Collaborating €
Made Easy
Chapter 22
Excel and the
Structured Query Language . . . . 467
Chapter 25
Excel and the Web . . . . . . . . . . . 519
443€
Chapter 21
Excel and Other Office
Applications . . . . . . . . . . . . . . . . 445
Chapter 26
Excel and the Extensible
Markup Language (XML) . . . . . . 539
vi
www.it-ebooks.info
Table of Contents
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
We’d Like to Hear from You . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
About the CD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
What’s o n the CD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Using the CD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
System Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv
Support Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv
Conventions and Features Used in this Book . . . . . . . . . . . . . . . . . . . . . . . . xxv
Text Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Design Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Part 1
Preliminaries
Chapter 1
What’s New in Excel 2003
3
Using Improved Statistical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Creating Smart Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Creating Document Workspaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Implementing Information Rights Management . . . . . . . . . . . . . . . . . . . . . . 7
Comparing Workbooks Side By Side. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Getting Information with the Research Task Pane . . . . . . . . . . . . . . . . . . . . 8
Using Extended XML Capabilities. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Creating Excel Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Mapping Excel Data to XML Schemas . . . . . . . . . . . . . . . . . . . . . . . 10
Publishing Spreadsheets with Web Components . . . . . . . . . . . . . . . . 11
Chapter 2
Exploring Excel
13
Workbooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Worksheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Cells and Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Excel Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Formulas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
14
15
18
21
vii
www.it-ebooks.info
Table of Contents
Part 2
Visual Basic for Applications
Chapter 3
Exploring Visual Basic for Applications
25
An Overview of Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . .
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Working with Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Recording and Viewing Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Running a Macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Debugging a Macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Implementing Macro Security and Digital Signatures . . . . . . . . . . . . .
25
26
27
27
28
29
29
31
34
36
Chapter 4
VBA Programming Starter Kit
41
Introducing the Visual Basic Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Opening the Visual Basic Editor. . . . . . . . . . . . . . . . . . . . . . . . . . . .
Recognizing Parts of the Visual Basic Editor . . . . . . . . . . . . . . . . . . .
Customizing the Visual Basic Editor . . . . . . . . . . . . . . . . . . . . . . . . .
Managing Code Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handling Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adding Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Notes on Programming Style and Code Readability . . . . . . . . . . . . . .
Developing Projects with the Project Explorer . . . . . . . . . . . . . . . . . . . . . .
Creating VBA Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Deleting VBA Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Copying Modules Between Projects . . . . . . . . . . . . . . . . . . . . . . . . .
Declaring Variables, Constants, and Data Types . . . . . . . . . . . . . . . . . . . .
Declaring Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Defining Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Defining Constants. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handling Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handling Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handling Variants and Data Type Conversions. . . . . . . . . . . . . . . . . .
Understanding Variable Scope and Lifetimes . . . . . . . . . . . . . . . . . .
Assigning Values to Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Working with Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Multidimensional Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Dynamic Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
viii
www.it-ebooks.info
41
41
41
49
53
53
53
54
55
55
56
57
57
58
58
59
59
60
61
62
64
64
65
65
66
Table of Contents
Writing to the Screen and Accepting Input . . . . . . . . . . . . . . . . . . . . . . . . 67
Creating a Message Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Creating an Input Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Defining Object Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Creating Custom Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
With...End With Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Controlling Program Flow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Selection Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
GoTo Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .82
Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Chapter 5
Creating Sub and Function Procedures
85
Defining Sub Procedures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Defining the Scope of a Sub Procedure . . . . . . . . . . . . . . . . . . . . . . 90
Running a Sub Procedure from Within Another Procedure . . . . . . . . . 93
Retaining Values Between Procedure Calls . . . . . . . . . . . . . . . . . . . . 98
Defining Function Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Creating a Function Procedure. . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Running Function Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Passing Arguments to Procedures . . . . . . . . . . . . . . . . . . . . . . . . . 101
Passing Named Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Organizing for Success . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Part 3
The Excel Object Model
Chapter 6
The Application Object
107
Introducing the Application Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Chapter 7
Workbooks and Worksheets
129
The Workbooks Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating New Workbooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Opening Workbooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Workbook Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Workbook Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Printing and Previewing Workbooks . . . . . . . . . . . . . . . . . . . . . . . .
129
129
130
138
145
147
ix
www.it-ebooks.info
Table of Contents
The Sheets and Worksheets Collections . . . . . . . . . . . . . . . . . . . . . . . . .
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Worksheet Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
149
149
150
156
Chapter 8
Ranges and Cells
161
Basic Range Manipulations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Finding the Active Range. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Selecting a Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Referring to Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Referencing Ranges on the Active Worksheet . . . . . . . . . . . . . . . . .
Referencing Ranges on an Inactive Worksheet . . . . . . . . . . . . . . . .
Referencing Cells in a Range. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Referencing Cells Using the Offset Property . . . . . . . . . . . . . . . . . .
Defining a Range Using the Cells Property . . . . . . . . . . . . . . . . . . .
Referencing Columns and Rows . . . . . . . . . . . . . . . . . . . . . . . . . .
Referencing Non-Contiguous Groups of Cells . . . . . . . . . . . . . . . . .
Manipulating Groups of Cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Resizing Cells Programmatically . . . . . . . . . . . . . . . . . . . . . . . . . .
Joining Two Ranges Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Detecting Empty Cells. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Named Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Defining a Named Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Changing Notation Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reserved Range Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Copying Data Between Ranges and Arrays . . . . . . . . . . . . . . . . . . .
Getting Data Entry Right the First Time . . . . . . . . . . . . . . . . . . . . . . . . .
161
161
162
166
166
167
167
167
172
172
172
172
172
173
176
177
177
179
182
185
187
Part 4
Advanced VBA
Chapter 9
Manipulating Data with VBA
193
Manipulating Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Determining if the Value in a Cell Is Text . . . . . . . . . . . . . . . . . . . .
Preparing String Data for Processing . . . . . . . . . . . . . . . . . . . . . . .
Determining the Number of Characters in a String . . . . . . . . . . . . .
Concatenating Text from Two or More Cells or Variables . . . . . . . . .
Returning the First or Last Several Characters from a String . . . . . .
Returning Characters from Arbitrary Positions in a String. . . . . . . . .
Finding a String Within Another String . . . . . . . . . . . . . . . . . . . . . .
Manipulating Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Performing Summary Calculations . . . . . . . . . . . . . . . . . . . . . . . . .
Performing Financial Calculations . . . . . . . . . . . . . . . . . . . . . . . . .
x
www.it-ebooks.info
193
193
195
197
198
200
202
202
206
206
208
Table of Contents
Manipulating Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Time and Date Serial Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Date and Time Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Chapter 10
Formatting Excel Objects
219
Using Excel Color Constants and the Color Palette . . . . . . . . . . . . . . . . .
Manipulating the Current Excel Color Palette . . . . . . . . . . . . . . . . .
Using the Excel Color Palette on the Web . . . . . . . . . . . . . . . . . . . .
Formatting Worksheet Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Formatting Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Formatting Cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Formatting Borders. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
219
223
226
230
231
233
236
Chapter 11
Creating Add-Ins and COM Add-Ins
239
Introducing Add-Ins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Add-Ins dialog box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Installing an Add-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Unloading an Add-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Excel Add-Ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating an Add-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Saving the Add-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Installing the Add-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the AddIns Collection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
AddIns Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
AddIn Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Automation and COM Add-Ins . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the IDTExtensibility2 Interface. . . . . . . . . . . . . . . . . . . . . . . .
Registry Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Building an Automation Add-In with Visual Basic 6 . . . . . . . . . . . . . . . . .
Designing the Add-In. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Registry Entries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Building a COM Add-In with Visual Basic .NET. . . . . . . . . . . . . . . . . . . . .
Running the Shared Add-In Wizard . . . . . . . . . . . . . . . . . . . . . . . . .
Modifying the Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Installing the Add-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
239
240
241
241
241
241
242
243
244
244
245
247
248
250
250
250
256
256
257
259
261
Chapter 12
Understanding and Using Events
265
Enabling and Disabling Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Workbook Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Open Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Activate Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
SheetActivate Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
266
267
269
270
270
xi
www.it-ebooks.info
Table of Contents
NewSheet Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
BeforeSave Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Deactivate Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
BeforePrint Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
BeforeClose Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Worksheet Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Change Event. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
SelectionChange Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
BeforeRightClick Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Application Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Turning on Application Event Monitoring . . . . . . . . . . . . . . . . . . . . .
Detecting When a Workbook Is Opened . . . . . . . . . . . . . . . . . . . . .
270
271
271
271
272
273
275
276
276
276
278
279
Chapter 13
Manipulating Files
281
Locating External Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Returning All Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Limiting the File Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Finding Files with the FileDialog Dialog Box . . . . . . . . . . . . . . . . . .
Writing to an External File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reading from an External File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Searching a File for a Value. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
281
282
285
291
295
296
298
Chapter 14
Developing Class Modules
299
What Is an Object? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
What Is a Class? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
What Are Properties? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
What Are Methods? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
What Are Events?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Introducing Class Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Accessing Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Declaring Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Objects and Nothing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Objects with Multiple Object Variables . . . . . . . . . . . . . . . . . . . . . .
Properties, Methods, and Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Public vs. Private Properties, Methods, and Events . . . . . . . . . . . . .
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Building a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a Class Module. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Defining Simple Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Defining Property Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Defining Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xii
www.it-ebooks.info
299
299
299
300
300
300
301
302
302
303
303
303
304
304
304
305
305
305
306
309
Table of Contents
Defining Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Defining Private Variables, Subroutines, and Functions . . . . . . . . . .
Special Events for Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Resolving References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practical Class Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Simple Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Extending a Simple Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Collection Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Class with Business Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
310
310
311
311
312
312
313
313
317
Part 5
Manipulating Excel Objects
Chapter 15
Charts
321
Introducing Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Embedded Charts or Chart Sheets . . . . . . . . . . . . . . . . . .
Defining the Chart Object Model . . . . . . . . . . . . . . . . . . . . . . . . . .
Manipulating Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Activating a Chart. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Deactivating a Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Modifying a Chart’s Data Series . . . . . . . . . . . . . . . . . . . . . . . . . .
Modifying a Chart to Use Data from Arrays . . . . . . . . . . . . . . . . . . .
Defining a Chart’s Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Formatting a Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Modifying All Charts in a Workbook . . . . . . . . . . . . . . . . . . . . . . . .
Printing Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Final Thoughts on Programming Charts . . . . . . . . . . . . . . . . . . . . . . . . .
321
322
326
327
327
330
331
333
335
336
338
339
339
Chapter 16
PivotTables and PivotCharts
341
PivotTables and PivotCharts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Introducing PivotTables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a PivotTable with the PivotTable Wizard . . . . . . . . . . . . . . .
Introducing PivotCharts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a PivotChart with the PivotTable Wizard . . . . . . . . . . . . . . .
Online Analytical Processing (OLAP) Issues . . . . . . . . . . . . . . . . . .
PivotTable Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
PivotTables Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
PivotTable Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
PivotCaches Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
PivotCache Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
PivotField Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
PivotItem Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
341
341
343
346
346
347
348
349
349
351
352
353
356
xiii
www.it-ebooks.info
Table of Contents
Programming PivotTables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a PivotTable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a PivotChart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Manipulating PivotTables Programmatically. . . . . . . . . . . . . . . . . . . . . . .
Pivoting a PivotTable Programmatically . . . . . . . . . . . . . . . . . . . . . .
Resetting a PivotTable to Its Original Position . . . . . . . . . . . . . . . . .
Recording and Restoring Arbitrary PivotTable Positions . . . . . . . . . .
356
357
358
359
359
361
361
Chapter 17
Command Bars
365
Identifying Parts of the Menu System. . . . . . . . . . . . . . . . . . . . . . . . . . .
CommandBars Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
CommandBar Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Listing CommandBar Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adding a Floating Command Bar . . . . . . . . . . . . . . . . . . . . . . . . . .
Deleting a Command Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Command Bar Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
CommandBarControls Collection . . . . . . . . . . . . . . . . . . . . . . . . . .
CommandBarControl Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Button Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Toolbars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Combo Box Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using a Combo Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Pop-Up Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Displaying a Pop-Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adding Items to an Existing Menu . . . . . . . . . . . . . . . . . . . . . . . . .
365
366
367
369
370
371
371
371
373
375
377
378
379
380
380
382
Chapter 18
Customizing Dialog Boxes
383
Displaying Existing Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Modifying Existing Dialog Boxes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exploring the Dialogs Collection . . . . . . . . . . . . . . . . . . . . . . . . . . .
Passing Arguments to Existing Dialog Boxes. . . . . . . . . . . . . . . . . .
Planning with Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
384
388
389
393
394
Chapter 19
Creating User Forms
395
Creating a UserForm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adding a UserForm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Designing a UserForm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Modifying a UserForm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Properties of a UserForm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Displaying a UserForm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xiv
www.it-ebooks.info
395
395
396
397
398
399
Table of Contents
UserForm Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Programming Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Common Properties, Methods, and Events. . . . . . . . . . . . . . . . . . .
The Label Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The CommandButton Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The TextBox Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The CheckBox Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The ToggleButton Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The SpinButton Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Frame Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The OptionButton Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Image Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The ScrollBar Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The ListBox Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The ComboBox Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The RefEdit Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The TabStrip Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The MultiPage Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
401
401
402
404
404
405
406
407
407
408
409
410
411
412
414
415
415
417
Chapter 20
Creating Advanced User Forms
419
Capturing Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Form Application Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Designing a Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Displaying Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Navigating the Worksheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Editing Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adding Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Validating Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Displaying the User Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Building a Multi-Step Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Wizard Application Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handling Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Building the UserForm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Navigating the Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Collecting Options for the Wizard . . . . . . . . . . . . . . . . . . . . . . . . . .
Summarizing the Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Running the Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
419
419
420
422
425
427
428
429
430
431
431
433
434
435
438
440
441
xv
www.it-ebooks.info
Table of Contents
Part 6
Excel and the Outside World: Collaborating Made Easy
Chapter 21
Excel and Other Office Applications
445
Starting Another Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Activating Another Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Late Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Early Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Interacting with Other Office Applications . . . . . . . . . . . . . . . . . . . . . . . .
Opening a Document in Word . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Accessing an Active Word Document . . . . . . . . . . . . . . . . . . . . . . .
Creating a New Word Document . . . . . . . . . . . . . . . . . . . . . . . . . .
Controlling Excel from Other Office Applications . . . . . . . . . . . . . . .
Working with Multiple Applications to Get the Job Done. . . . . . . . . . . . . .
445
449
449
450
452
455
457
460
461
462
465
Chapter 22
Excel and the Structured Query Language
467
Comparing Spreadsheets and Databases. . . . . . . . . . . . . . . . . . . . . . . .
Fundamental Database Concepts . . . . . . . . . . . . . . . . . . . . . . . . .
Database Keys. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Accessing Databases from Excel . . . . . . . . . . . . . . . . . . . . . . . . . .
Manipulating Databases with SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Select Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Simple Select Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Retrieving Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Sorting Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Multiple Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Insert Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Insert Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Update Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Delete Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
467
467
469
470
471
471
472
473
475
476
477
478
478
479
479
Chapter 23
Introducing ADO
481
The ADO Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the ADO Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Connection Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Key Properties and Methods of the Connection Object. . . . . . . . . . .
Connecting to Different Database Management Systems . . . . . . . .
Using the Errors Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Error Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xvi
www.it-ebooks.info
482
483
483
484
485
486
487
Table of Contents
The Command Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Key Properties and Methods of the Command Object . . . . . . . . . . .
Using the Parameters Collection . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Parameter Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Recordset Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Key Properties and Methods of the Recordset Object . . . . . . . . . . .
Using the Fields Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Field Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
487
488
489
490
491
491
494
494
Chapter 24
Excel Query Program
497
Excel Query Program Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . .
Setting Up the Project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Initializing the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Ending the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Connecting to a Database. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Initializing the DBInfo UserForm . . . . . . . . . . . . . . . . . . . . . . . . . . .
Changing Database Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Selecting Windows Authentication . . . . . . . . . . . . . . . . . . . . . . . . .
Saving the Database Info . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Editing a Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Executing a Database Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Getting the Information to Run the Query . . . . . . . . . . . . . . . . . . . .
Building a Connection String . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Getting the Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Running a Query. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Copying Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Excel Query Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Configure the Connection Information . . . . . . . . . . . . . . . . . . . . . .
Enter a Query. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Run the Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
497
498
499
500
501
502
503
504
504
505
508
508
509
512
512
514
515
516
516
517
Chapter 25
Excel and the Web
519
HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Saving a Worksheet as a Web Page . . . . . . . . . . . . . . . . . . . . . . . .
Publishing a Worksheet to the Web . . . . . . . . . . . . . . . . . . . . . . . .
Making Web-Based Worksheets Interactive. . . . . . . . . . . . . . . . . . .
Using the Internet as a Data Source . . . . . . . . . . . . . . . . . . . . . . . . . . .
Opening Web Pages as Workbooks . . . . . . . . . . . . . . . . . . . . . . . .
Using Web Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Parsing Web Pages for Specific Information . . . . . . . . . . . . . . . . . .
Using the Internet to Publish Results . . . . . . . . . . . . . . . . . . . . . . . . . . .
Setting Up a Web Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Saving Worksheets as Web Pages . . . . . . . . . . . . . . . . . . . . . . . . .
520
520
523
525
527
528
529
532
533
533
534
xvii
www.it-ebooks.info
Table of Contents
Adding Interactivity with the Web Components . . . . . . . . . . . . . . . . 534
Communicating with a Web Server. . . . . . . . . . . . . . . . . . . . . . . . . 536
Using Internet Solutions with Excel . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
Chapter 26
Excel and the Extensible Markup Language (XML)
539
Introducing Data Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Data Lists Programmatically. . . . . . . . . . . . . . . . . . . . . . .
Creating XML Schemas. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating XML Data Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adding XML to a Workbook Manually . . . . . . . . . . . . . . . . . . . . . . . . . . .
Importing XML Data Manually . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adding XML to a Worksheet Programmatically. . . . . . . . . . . . . . . . . . . . .
Mapping a Schema to a Worksheet Programmatically . . . . . . . . . . .
Mapping Schema Elements to Cells Using XPath . . . . . . . . . . . . . .
xviii
www.it-ebooks.info
539
541
546
549
550
552
553
553
557
Acknowledgments
From Curtis Frye:
First and foremost I’d like to thank my co-authors, Wayne Freeze and Felicia Buckingham,
for their knowledge and skill at presenting a series of complex topics so effectively. Westley
Annis pitched in on several chapters as well, for which I am grateful.
I value my relationship with Microsoft Press for many reasons, but right up there on the
list is the terrific working relationship I have with everyone there. I’m happy to say our
relationship survived intact despite many changes in all of our lives that transpired during
this project. Juliana Aldous Atkinson invited me to submit an outline and shepherded the
proposal through the review process. Sandra Haynes was the first project editor, and Dick
Brown took over when Sandra assumed different duties within Press. Wendy Zucker
stepped in to help during the revision process when Dick went on vacation for a week. The
phrase “team effort” doesn’t begin to describe the sort of support they provided.
I was also very fortunate to have Mark Dodge, co-author of Microsoft Office Excel 2003
Inside Out, as my technical editor. His knowledge of both the series and Excel gave him the
perspective to make many terrific recommendations. Lisa Pawlewicz, ably assisted by
Jaime Odell, performed what was probably one of the more involved copy edits that has
passed over their desks in a while. Page proofs were handled by Sandi Resnick at Microsoft
and the nSight team of Joe Armstrong, Steve Boudreault, Catherine Cooker, Beth Lew,
Katie O’Connell, Dan Shaw, Asa Tomash, and Melissa von Tschudi-Sutton. Barbara Levy
and Carl Diltz turned my Word files into the beautiful pages you see before you, William
Teel got the graphics ready for production, Joel Panchot created the original art, and
Richard Shrout created a thorough index. Tess McMillan heads the team that created the
Companion CD.
Finally, I’d like to thank my agent, Neil Salkind of StudioB, for introducing me to
Microsoft back in December of 2000. He was concerned about recommending a relatively
new author to such an important publisher, but I’d say things have turned out pretty well.
From Wayne Freeze:
Curt: thanks for putting up with me while writing this book. I valued your patience, especially when I was running behind schedule as usual. Laura: thank you for making my first
year at StudioB a very worthwhile experience. Steve: thank you for the opportunities and
challenges you tossed my way this year and I have even higher expectations for the next.
Christopher and Samantha: thanks for tolerating the long hours I’ve spent working on this
book alongside my other projects. Jill: thank you for being you—I love you.
From Felicia Buckingham:
Thanks to everyone from Microsoft Press for their work on the project, and thanks to Curt
for the opportunity to co-author my first book.
xix
www.it-ebooks.info
www.it-ebooks.info
We’d Like to Hear from You
Our goal at Microsoft Press is to create books that help you find the information you need to
get the most out of your software.
The Inside Out series was created with you in mind. As part of our ongoing effort to ensure
that we’re creating the books that meet your learning needs, we’d like to hear from you. Let us
know what you think. Tell us what you like about this book and what we can do to make it
better. When you write, please include the title and author of this book in your e-mail mes€
sage, as well as your name and contact information. We look forward to hearing from you!
How to Reach Us
E-Mail:
Mail:
nsideout@microsoft.com
Inside Out Series Editor
Microsoft Press
One Microsoft Way
Redmond, WA 98052
Note: Unfortunately, we can’t provide support for any software problems you might experience.
Please go to http://support.microsoft.com for help with any software issues.
xxi
www.it-ebooks.info
www.it-ebooks.info
About the CD
The Companion CD that ships with this book contains many tools and resources to help you
get the most out of your Inside Out book.
What’s on the CD
Your Inside Out CD includes the following:
●
Complete eBook.
In this section, you’ll find an electronic version of Microsoft Office
Excel 2003 Programming Inside Out. The eBook is in PDF format.
●
Computer Dictionary, Fifth Edition eBook.
Here you’ll find the full electronic
version of the Microsoft Computer Dictionary, Fifth Edition. Suitable for home and
office, the dictionary contains more than 10,000 entries.
●
Insider Extras.
This section includes the sample files used in the book.
●
Microsoft Resources.
In this section, you’ll find information about additional
resources from Microsoft that will help you get the most out of Microsoft Office
Excel 2003 and other business software from Microsoft.
●
Extending Excel.
In this section, you’ll find great information about third-party
utilities and tools you use to further enhance your experience with Microsoft Office
Excel 2003.
The Companion CD provides detailed information about the files on this CD, and links to
Microsoft and third-party sites on the Internet. All the files on this CD are designed to be
accessed through Microsoft Internet Explorer (version 5.01 or later).
Note
The links to third-party sites are not under the control of Microsoft Corporation, and
Microsoft is therefore not responsible for their content, nor should their inclusion on this
CD be construed as an endorsement of the product or the site. Software provided on this
CD is in English language only and may be incompatible with non-English language operating systems and software.
Using the CD
To use this Companion CD, insert it into your CD-ROM drive. If AutoRun is not enabled on
your computer, click on Index.htm in the WebSite folder in the root of the CD.
xxiii
www.it-ebooks.info
About the CD
System Requirements
Following are the minimum system requirements necessary to run the CD:
●
Microsoft Windows XP or later or Windows 2000 Professional with Service Pack 3 or
later.
●
266-MHz or higher Pentium-compatible CPU
●
64 megabytes (MB) RAM
●
8X CD-ROM drive or faster
●
Microsoft Windows–compatible sound card and speakers
●
Microsoft Internet Explorer 5.01 or later
●
Microsoft Mouse or compatible pointing device
Note System requirements may be higher for the add-ins available via links on the CD.
Individual add-in system requirements are specified at the sites listed. An Internet connection is necessary to access some of the hyperlinks. Connect time charges may apply.
Support Information
Every effort has been made to ensure the accuracy of the book and the contents of this
Companion CD. For feedback on the book content or this Companion CD, please contact us
by using any of the addresses listed in the “We’d Like to Hear From You” section.
Microsoft Press provides corrections for books through the World Wide Web at
http://www.microsoft.com/learning/support/. To connect directly to the Microsoft Press
Knowledge Base and enter a query regarding a question or issue that you may have, go to
http://www.microsoft.com/learning/support/search.asp.
For support information regarding Windows XP, you can connect to Microsoft Technical
Support on the Web at http://support.microsoft.