[Office.Books].Visual.Basic.For.Application.Unleashed.pdf

(31981 KB) Pobierz
Visual Basic for Applications Unleashed
i
Paul McFedries
201 West 103rd Street
Indianapolis, IN 46290
184216871.002.png
ii
Visual Basic for Applications
U NLEASHED
Copyright © 1997 by Sams Publishing
FIRST EDITION
All rights reserved. No part of this book shall be reproduced, stored in a
retrieval system, or transmitted by any means, electronic, mechanical,
photocopying, recording, or otherwise, without written permission from the
publisher. No patent liability is assumed with respect to the use of the
information contained herein. Although every precaution has been taken in
the preparation of this book, the publisher and author assume no responsi-
bility for errors or omissions. Neither is any liability assumed for damages
resulting from the use of the information contained herein. For informa-
tion, address Sams Publishing, 201 W. 103rd St., Indianapolis, IN 46290.
International Standard Book Number: 0-672-31046-5
Library of Congress Catalog Card Number: 96-72008
2000 99 98 97 4 3 2 1
Interpretation of the printing code: the rightmost double-digit number is
the year of the book’s printing; the rightmost single-digit, the number of
the book’s printing. For example, a printing code of 97-1 shows that the
first printing of the book occurred in 1997.
Composed in AGaramond and MCPdigital by Macmillan Computer
Publishing
Printed in the United States of America
Acquisitions Editor
Dean Miller
Development Editor
Brian-Kent Proffitt
Software Development Specialists
Patricia J. Brooks
John Warriner
Production Editor
Gayle L. Johnson
Copy Editor
Bart Reed
Indexers
Bruce Clingaman
Erika Millen
Technical Reviewer
John Charlesworth
Formatter
Katie Wise
Technical Edit Coordinator
Lynette Quinn
Editorial Assistants
Carol Ackerman
Andi Richter
Rhonda Tinch-Mize
Cover Designer
Jason Grisham
Book Designer
Gary Adair
Production Team Supervisors
Brad Chinn
Charlotte Clapp
Production
Georgiana Briggs
Shawn Ring
Janet Seib
Mary Ellen Stephenson
Trademarks
All terms mentioned in this book that are known to be trademarks or
service marks have been appropriately capitalized. Sams Publishing cannot
attest to the accuracy of this information. Use of a term in this book should
not be regarded as affecting the validity of any trademark or service mark.
Publisher and President Richard K. Swadley
Publishing Manager Dean Miller
Director of Editorial Services Cindy Morrow
Managing Editor Mary Inderstrodt
Director of Marketing Kelli S. Spencer
Assistant Marketing Managers Kristina Perry
Rachel Wolfe
For Karen (purr)
184216871.003.png
iv
Visual Basic for Applications
U NLEASHED
23 Trapping Program Errors 653
24 Debugging VBA Procedures 669
26 VBA Tips and Techniques 727
Part VIII Unleashing VBA Applications
28 Making Backups as You Work 799
A VBA Statements 865
B VBA Functions 873
C The Windows ANSI Character Set 883
D An HTML Primer 891
Index 913
184216871.005.png
Contents
v
Introduction xxvi
Part I Unleashing the VBA Programming Language
1 Introducing VBA 3
What Is a Macro? ............................................................................. 4
How Does VBA Fit In? ............................................................... 4
The Three Types of Procedures ................................................... 5
Recording a VBA Macro .................................................................. 5
Viewing the Resulting Module .................................................... 7
Editing a Recorded Macro ........................................................... 8
Touring the Visual Basic Editor ....................................................... 8
The Project Explorer .................................................................... 8
The Properties Window ............................................................... 9
The Work Area .......................................................................... 10
Writing Your Own Command Macro ............................................ 11
Creating a New Module ............................................................ 11
Writing a Command Macro ...................................................... 12
Running a VBA Macro .................................................................. 13
Creating User-Defined Functions with VBA .................................. 13
Understanding User-Defined Functions .................................... 14
Writing User-Defined Functions ............................................... 16
Employing User-Defined Functions .......................................... 16
Working with Procedures ............................................................... 17
The Structure of a Procedure ..................................................... 17
Calling a Procedure ................................................................... 18
Public Versus Private Procedures ............................................... 20
Another Method for Inserting a Procedure ................................ 20
Visual Basic Editor Techniques for Easier Coding ..................... 21
Finding Code ............................................................................ 26
Navigating Modules with Bookmarks ........................................ 29
Working with Modules .................................................................. 30
Renaming a Module .................................................................. 30
Exporting a Module ................................................................... 30
Importing a Module .................................................................. 31
Removing a Module .................................................................. 32
Setting Visual Basic Editor Options ............................................... 32
The Editor Tab .......................................................................... 33
The Editor Format Tab ............................................................. 34
The General Tab ....................................................................... 34
The Docking Tab ...................................................................... 35
184216871.001.png
Zgłoś jeśli naruszono regulamin