microsoft office excel 2003 programming inside out

590 920 0
microsoft office excel 2003 programming inside out

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Cover.fm Page 1 Tuesday, January 13, 2004 4:23 PM 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, 1968- Microsoft 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 Microsoft Press Confidential masterpage = “blank” Inside Out (FrameMaker) sample pages jimkr To our families www.it-ebooks.info www.it-ebooks.info Contents at a Glance Part 1 Preliminaries 1€ Chapter 1 What’s New in Excel 2003 . . . . . . . 3 Chapter 2 Exploring Excel . . . . . . . . . . . . . . 13 Part 2 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 Part 3 The Excel € Object Model 105€ Chapter 6 The Application Object . . . . . . . . 107 Chapter 7 Workbooks and Worksheets . . . . 129 Chapter 8 Ranges and Cells . . . . . . . . . . . . 161 Part 4 Advanced VBA 191€ Chapter 9 Manipulating Data with VBA. . . . 193 Chapter 10 Formatting Excel Objects . . . . . . 219 Chapter 11 Creating Add-Ins and COM Add-Ins . . . . . . . . . . . . . . . 239 Chapter 12 Understanding and Using Events . . . . . . . . . . . . . . . 265 Chapter 13 Manipulating Files . . . . . . . . . . . 281 Chapter 14 Developing Class Modules . . . . . 299 Part 5 Manipulating € Excel Objects 319€ Chapter 15 Charts . . . . . . . . . . . . . . . . . . . . 321 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 Part 6 Excel and the € Outside World: € Collaborating € Made Easy 443€ Chapter 21 Excel and Other Office Applications . . . . . . . . . . . . . . . . 445 Chapter 22 Excel and the Structured Query Language . . . . 467 Chapter 23 Introducing ADO . . . . . . . . . . . . 481 Chapter 24 Excel Query Program . . . . . . . . . 497 Chapter 25 Excel and the Web . . . . . . . . . . . 519 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 on 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Worksheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Cells and Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 The Excel Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Formulas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 . . . . . . . . . . . . . . . . . . . . . . 25 Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Working with Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Recording and Viewing Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Running a Macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Debugging a Macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Implementing Macro Security and Digital Signatures . . . . . . . . . . . . . 36 Chapter 4 VBA Programming Starter Kit 41 Introducing the Visual Basic Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Opening the Visual Basic Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Recognizing Parts of the Visual Basic Editor . . . . . . . . . . . . . . . . . . . 41 Customizing the Visual Basic Editor . . . . . . . . . . . . . . . . . . . . . . . . . 49 Managing Code Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Handling Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Adding Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Notes on Programming Style and Code Readability . . . . . . . . . . . . . . 54 Developing Projects with the Project Explorer . . . . . . . . . . . . . . . . . . . . . . 55 Creating VBA Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Deleting VBA Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Copying Modules Between Projects . . . . . . . . . . . . . . . . . . . . . . . . . 57 Declaring Variables, Constants, and Data Types . . . . . . . . . . . . . . . . . . . . 57 Declaring Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Defining Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Defining Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Handling Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Handling Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Handling Variants and Data Type Conversions. . . . . . . . . . . . . . . . . . 61 Understanding Variable Scope and Lifetimes . . . . . . . . . . . . . . . . . . 62 Assigning Values to Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Working with Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Creating an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Creating Multidimensional Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Creating Dynamic Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 viii www.it-ebooks.info 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Creating New Workbooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Opening Workbooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Workbook Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 Workbook Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Printing and Previewing Workbooks . . . . . . . . . . . . . . . . . . . . . . . . 147 ix www.it-ebooks.info Table of Contents The Sheets and Worksheets Collections . . . . . . . . . . . . . . . . . . . . . . . . . 149 Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Worksheet Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Chapter 8 Ranges and Cells 161 Basic Range Manipulations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Finding the Active Range. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Selecting a Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Referring to Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Referencing Ranges on the Active Worksheet . . . . . . . . . . . . . . . . . 166 Referencing Ranges on an Inactive Worksheet . . . . . . . . . . . . . . . . 167 Referencing Cells in a Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Referencing Cells Using the Offset Property . . . . . . . . . . . . . . . . . . 167 Defining a Range Using the Cells Property . . . . . . . . . . . . . . . . . . . 172 Referencing Columns and Rows . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Referencing Non-Contiguous Groups of Cells . . . . . . . . . . . . . . . . . 172 Manipulating Groups of Cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Resizing Cells Programmatically . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Joining Two Ranges Together. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Detecting Empty Cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Using Named Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Defining a Named Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Changing Notation Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Reserved Range Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Copying Data Between Ranges and Arrays . . . . . . . . . . . . . . . . . . . 185 Getting Data Entry Right the First Time . . . . . . . . . . . . . . . . . . . . . . . . . 187 Part 4 Advanced VBA Chapter 9 Manipulating Data with VBA 193 Manipulating Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Determining if the Value in a Cell Is Text . . . . . . . . . . . . . . . . . . . . 193 Preparing String Data for Processing . . . . . . . . . . . . . . . . . . . . . . . 195 Determining the Number of Characters in a String . . . . . . . . . . . . . 197 Concatenating Text from Two or More Cells or Variables . . . . . . . . . 198 Returning the First or Last Several Characters from a String . . . . . . 200 Returning Characters from Arbitrary Positions in a String . . . . . . . . . 202 Finding a String Within Another String . . . . . . . . . . . . . . . . . . . . . . 202 Manipulating Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Performing Summary Calculations . . . . . . . . . . . . . . . . . . . . . . . . . 206 Performing Financial Calculations . . . . . . . . . . . . . . . . . . . . . . . . . 208 x www.it-ebooks.info [...]... 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. .. 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. .. Preliminaries Microsoft Office Excel 2003 Programming Inside Out Creating Smart Documents Chapter 1 Excel workbooks have always been able to interact with other Office documents, but those interactions have been somewhat limited Creating links to data on the Web, in a database, or in another Office document are all valuable abilities, but the workbook was simply a standalone collection of bits In Office 2003, ... of the file but don’t have Office 2003 or later installed on their computer will be able to use file viewers that let them examine the files, but not edit them Note Information Rights Management is only available in Microsoft Office Professional Edition 2003, Word 2003, Excel 2003, and PowerPoint 2003 Comparing Workbooks Side By Side Another useful new capability in Excel 2003 is the ability to scroll... New in Excel 2003 Chapter 1 document become available for every member of the team; if the changes conflict, you as the administrator are able to choose which changes will be adopted You can also use the controls on the Shared Workspace task pane to create and manage a document workspace Note Document workspaces are available for Microsoft Office Word 2003, Excel 2003, PowerPoint 2003, and Visio 2003. .. www.it-ebooks.info Part 1: Preliminaries Microsoft Office Excel 2003 Programming Inside Out Table 1-1 Improved Statistical Functions Chapter 1 BINOMDIST Determines the probability that a set number of true/false trials, where each trial has a consistent chance of generating a true or false result, will result in exactly a specified number of successes (for example, exactly five out of ten coin flips will end... 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... you can filter the data, create a PivotTable, or add new rows to the list Whichever task you perform, Excel will shift the worksheet contents around the list to reflect the new entry (such as by adding a blank row 9 www.it-ebooks.info Part 1: Preliminaries Microsoft Office Excel 2003 Programming Inside Out Chapter 1 when you add data to a list) If you are working on a network equipped with SharePoint... 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,... For box) to get more infor­ mation Figure 1-1 shows the Research task pane and a selection of the reference materials available for you 7 www.it-ebooks.info Part 1: Preliminaries Microsoft Office Excel 2003 Programming Inside Out Getting Information with the Research Task Pane Chapter 1 Figure 1-1 You can find synonyms, look up words in encyclopedias, and translate words into foreign languages using . any means without the written permission of the publisher. Library of Congress Cataloging-in-Publication Data Frye, Curtis, 1968- Microsoft Office Excel 2003 Programming Inside Out / Curtis. 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. . 419 Part 6 Excel and the € Outside World: € Collaborating € Made Easy 443€ Chapter 21 Excel and Other Office Applications . . . . . . . . . . . . . . . . 445 Chapter 22 Excel and the Structured

Ngày đăng: 28/04/2014, 16:27

Từ khóa liên quan

Mục lục

  • Cover

    • Copyright page

    • Dedication

    • Contents at a Glance

    • Table of Contents

    • Acknowledgments

    • We’d Like to Hear from You

    • About the CD

      • What’s on the CD

      • Using the CD

      • System Requirements

      • Support Information

      • Conventions and Features Used in this Book

        • Text Conventions

        • Design Conventions

        • Part 1 Preliminaries

          • Chapter 1 What’s New in Excel 2003

            • Using Improved Statistical Functions

            • Creating Smart Documents

            • Creating Document Workspaces

            • Implementing Information Rights Management

            • Comparing Workbooks Side By Side

            • Getting Information with the Research Task Pane

            • Using Extended XML Capabilities

              • Creating Excel Lists

              • Mapping Excel Data to XML Schemas

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan