Part One An Intuitive Approach: Chapter 1 Writing Your First Macro

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

*1038_ch00_CMP3 6/21/03 12:59 PM Page v

Contents
Preface...................................................................................................................xiii
The Idea Behind This Book .............................................................................xv

Part One
An Intuitive Approach ..................................................................1

Chapter 1 Writing Your First Macro ..........................................3

1.1 Definition of Terms ..................................................................................4


1.2 What Is Visual Basic for Applications? .........................................6
1.3 Linking a Custom Style with a Tool ..................................................9
1.4 A Macro for Simplifying Input ..........................................................15
1.5 A Simple Literature Database ............................................................17
1.6 A Form for Computing Interest in a Savings Account .............24
1.7 User-Defined Functions ..........................................................................30
1.8 Analysis of Complex Tables .................................................................31
1.9 A Vocabulary Tutor ..................................................................................32
1.10 Example Programs for You to Try ....................................................40

Chapter 2 What’s New in Excel 2000 ........................................47

2.1 New in Excel 2002 .....................................................................................48


2.2 New in Excel 2000 .....................................................................................51
2.3 New in Excel 97 .........................................................................................54
2.4 New in Excel 7 ...........................................................................................59
2.5 Problems and Incompatibilities ........................................................59

v
*1038_ch00_CMP3 6/21/03 12:59 PM Page vi

Contents

Part Two
Fundamentals .............................................................................................65

Chapter 3 The Development Environment .................................67


Chapter Overview ................................................................................................67
3.1 The Components of a VBA Program ......................................................68
3.2 The Components of the Development Environment .......................69
Object Browser .................................................................................................72
3.3 Code Input in Modules ............................................................................76
3.4 Executing Macros .......................................................................................80
3.5 Recording Macros .......................................................................................82
3.6 Keyboard Shortcuts ..................................................................................83

Chapter 4 VBA Concepts ........................................................................85


4.1 Variables and Fields ..............................................................................86
Managing Variables ..........................................................................................86
Syntax Summary ...............................................................................................95
4.2 Procedural Programming ..........................................................................97
Procedures and Parameters .............................................................................97
Realm of Validity of Variables and Procedures .............................................107
Branching (Tests) ...........................................................................................112
Loops ...............................................................................................................115
Syntax Summary .............................................................................................119
4.3 Objects .........................................................................................................123
Working with Objects, Methods, and Properties .........................................123
The Object Browser (References) ..................................................................129
Accessing Objects Clarified with Keyword With ..........................................134
Object Variables ..............................................................................................135
Syntax Summary .............................................................................................138
4.4 Events ...........................................................................................................139
Event Procedures ............................................................................................140
Overview of Important Excel Events .............................................................144
Receiving Events of Arbitrary Objects ...........................................................150
Generating Event Procedures with Program Code ......................................152
Syntax Summary .............................................................................................156
4.5 Programming Your Own Classes ...........................................................159
Custom Methods, Properties, and Events ....................................................161
Collection Object ............................................................................................165
Example for a Class Module ..........................................................................167

vi
*1038_ch00_CMP3 6/21/03 12:59 PM Page vii

Contents

Example for Derived Classes (Implements) .................................................168


Syntax Summary .............................................................................................173
4.6 Operators in VBA .....................................................................................175
4.7 Virus Protection .....................................................................................178

Chapter 5 Techniques of Programming.....................................185


5.1 Cells and Ranges of Cells .................................................................186
Objects, Methods, Properties ........................................................................186
Programming Techniques ..............................................................................201
Syntax Summary .............................................................................................212
5.2 Workbooks, Windows, and Worksheets ..............................................215
Objects, Methods, and Properties .................................................................215
Programming Techniques ..............................................................................221
Syntax Summary .............................................................................................226
5.3 Data Transfer via the Clipboard ....................................................228
Copying, Cutting, and Pasting Ranges of Cells ............................................228
Access to the Clipboard with the DataObject ..............................................230
Syntax Summary .............................................................................................231
5.4 Working with Numbers and Character Strings ............................232
Numerical Functions, Random Numbers ....................................................232
Character Strings ............................................................................................235
Transformation Functions .............................................................................241
Syntax Summary .............................................................................................243
5.5 Working with Dates and Times ...........................................................246
VBA Functions ................................................................................................250
Worksheet Functions .....................................................................................253
Application and Programming Techniques .................................................254
Holidays ..........................................................................................................257
Syntax Summary .............................................................................................265
5.6 Working with Files; Text Import and Export ............................267
File System Objects: An Overview .................................................................267
Drives, Folders, and Files ...............................................................................269
Text Files (TextStream) ...................................................................................276
Binary Files (Open) .........................................................................................278
Excel-Specific Methods and Properties ........................................................283
Importing and Exporting Text Files ..............................................................286
Text Exportation for Mathematica Lists .......................................................296
Syntax Summary .............................................................................................301
5.7 User-Defined Worksheet Functions ..................................................306
Fundamentals .................................................................................................306
Examples .........................................................................................................314

vii
*1038_ch00_CMP3 6/21/03 12:59 PM Page viii

Contents

5.8 Protection Mechanisms ..........................................................................317


Restricting the Field of Action .......................................................................318
Protecting Cells and Worksheets from Changes ..........................................319
Protection Mechanism for Shared Workbooks ............................................324
Protecting Program Code and ToolBars .......................................................324
Syntax Summary .............................................................................................326
5.9 Configuration Files, Custom Configuration ..............................327
Options ............................................................................................................327
Setting Options with Program Code .............................................................328
Configuration Files .........................................................................................332
5.10 Excel Meets the Euro ..........................................................................339
The Euroconvert Function ............................................................................340
Converting Excel Files to the Euro ................................................................343
5.11 Tips and Tricks .....................................................................................356
Speed Optimization .......................................................................................356
Time-Intensive Calculations .........................................................................357
Working Efficiently with Worksheets ............................................................362
Interplay with Excel-4 Macros .......................................................................365
Determining the Version of Excel ..................................................................366
Helping You to Help Yourself .........................................................................366
Syntax Summary .............................................................................................368

Chapter 6 Debugging, Protection When Errors Arise ..371


Chapter Overview ..............................................................................................371
6.1 Debugging Aids .........................................................................................372
Syntax Control ................................................................................................372
Reaction to Errors ...........................................................................................373
Controlled Program Execution ......................................................................376
6.2 Error-Tolerant Behavior of Programs ...........................................380
6.3 Response to Program Interrupts ......................................................385
6.4 Syntax Summary .........................................................................................386

Chapter 7 Forms (Microsoft Forms Library) .....................387


7.1 Predefined Dialogs ................................................................................388
Standard Excel Dialogs ...................................................................................388
The Functions MsgBox and InputBox ..........................................................392
The Method Application.InputBox ...............................................................393
7.2 User-Defined Forms ................................................................................395
Changes with Respect to Excel 5/7 ...............................................................396
Introductory Example ....................................................................................398

viii
*1038_ch00_CMP3 6/21/03 12:59 PM Page ix

Contents

7.3 The Form Editor .......................................................................................402


7.4 The MS Forms Controls ..........................................................................406
Label ................................................................................................................408
TextBox ............................................................................................................409
ListBox and ComboBox ..................................................................................412
Check Box and OptionButton .......................................................................419
Command Buttons and Toggle Buttons .......................................................421
Frames .............................................................................................................422
MultiPage, TabStrip ........................................................................................424
Scrollbar and SpinButton ..............................................................................429
Image ...............................................................................................................431
Formulas (RefEdit) .........................................................................................432
The UserForm Object .....................................................................................434
7.5 Placing Controls Directly into Worksheets ..............................438
7.6 Programming Techniques ........................................................................447
Numerical Input .............................................................................................447
Reciprocal Calling of Forms ...........................................................................449
Editing Forms Dynamically ...........................................................................451
Working with Spin Buttons ............................................................................453

Chapter 8 Menus and Toolbars.......................................................457


8.1 Menu Bars and Toolbars ........................................................................458
Manual Editing of Menus and Toolbars ........................................................458
Saving Changes ...............................................................................................464
Object Hierarchy ............................................................................................466
Programming Techniques ..............................................................................472
Changing Sheets via a Toolbar ......................................................................479
Differences Compared to Excel 5/7 ..............................................................482
Syntax Summary .............................................................................................483
8.2 Formatting the Interface for Freestanding
Excel Applications .........................................................................................485
Extending the Standard Menu .......................................................................486
Hiding and Unhiding Custom Toolbars ........................................................489
Using a Custom Standard Menu ...................................................................490

ix
*1038_ch00_CMP3 6/21/03 12:59 PM Page x

Contents

Part III
Application ..............................................................................................493

Chapter 9 Templates, Smart Forms ...........................................495

9.1 Fundamentals ..............................................................................................496


Design Elements for Smart Forms ................................................................497
Templates Linked to Databases ....................................................................505
9.2 Example: The “Speedy” Invoice Form ..............................................509
9.3 Example: Balance Sheet for a Car-Sharing Club .....................518
9.4 The Limits of Smart Forms .................................................................525

Chapter 10 Charts and Drawing Objects (Shapes) ...........527


10.1 Charts .........................................................................................................528
Fundamentals .................................................................................................528
Chart Types .....................................................................................................529
Chart Elements (Chart Objects) and Formatting Options ..........................530
Printing ............................................................................................................534
10.2 Programming Charts ..............................................................................535
Object Hierarchy ............................................................................................536
Programming Techniques ..............................................................................540
10.3 Example: Automatic Data Reporting ..............................................544
Using the Example Program ..........................................................................544
Program Code .................................................................................................547
10.4 Syntax Summary for Charts ...............................................................558
10.5 Drawing Objects (Shapes) .................................................................560
10.6 Diagrams ....................................................................................................565

Chapter 11 Data Management in Excel .......................................569


11.1 Fundamentals ...........................................................................................570
Introduction: What Is a Database? ................................................................570
A Brief Database Glossary ..............................................................................571
Excel Versus Database Systems .....................................................................572
11.2 Data Management Within Excel ........................................................575
Creating a Database in Excel .........................................................................576
Input, Alteration, and Deletion of Data with the Database Form ..............579
Sorting, Searching, and Filtering Data ..........................................................581
11.3 Data Processing in VBA Code ...........................................................588
Programming Techniques ..............................................................................588

x
*1038_ch00_CMP3 6/21/03 12:59 PM Page xi

Contents

Example: A Form Letter in Microsoft Word ..................................................591


Syntax Summary .............................................................................................594
11.4 Database Worksheet Functions ........................................................595
11.5 Consolidating Tables ..........................................................................599
Fundamentals .................................................................................................599
Consolidation via VBA Code ..........................................................................601
11.6 Example: Balance Sheet for a Car-Sharing Club ...................602
Running the Program .....................................................................................603
Overview of the Components of the Application ........................................607
Program Code .................................................................................................608

Chapter 12 Access to External Data ..........................................619


12.1 Fundamental Concepts About Relational Databases ...............620
12.2 Importing External Data ...................................................................626
Importing Data from Databases (MS Query) ...............................................627
The QueryTable Object ..................................................................................639
Exporting Excel Data ......................................................................................643
12.3 Database Access with the ADO Library .......................................645
Introduction ....................................................................................................645
Connections ....................................................................................................650
Recordset .........................................................................................................654
SQL Commands (Command) ........................................................................662
SQL Basics .......................................................................................................663
Syntax Summary .............................................................................................666
12.4 Example: Evaluating a Questionnaire .........................................669
Overview .........................................................................................................669
Construction of the Database .......................................................................674
Program Code .................................................................................................675

Chapter 13 Data Analysis in Excel ............................................687


13.1 Grouping Data (Subtotals) ...............................................................688
Introduction ....................................................................................................688
Programming ..................................................................................................689
13.2 Pivot Tables ...........................................................................................692
Introduction ....................................................................................................692
Layout Options ...............................................................................................697
Pivot Tables for External Data .......................................................................703
Pivot Table Options ........................................................................................708
Pivot Charts .....................................................................................................709

xi
*1038_ch00_CMP3 6/21/03 12:59 PM Page xii

Contents

13.3 Programming Techniques ......................................................................710


Generating and Deleting Pivot Tables ..........................................................711
Construction and Reconstruction of Existing Pivot Tables .........................716
Internal Management (PivotCache) .............................................................720
Syntax Summary .............................................................................................728

Chapter 14 VBA Programming for Pros .......................................731


14.1 Add-Ins .......................................................................................................732
14.2 Excel and the Internet ......................................................................737
Sending Excel Files as E-Mail ........................................................................737
HTML Import .................................................................................................739
HTML Export, Web Components ..................................................................740
14.3 Smart Tags ................................................................................................744
14.4 Using Web Services ..............................................................................747
14.5 Using Dynamic Link Libraries (DLLs) .........................................754
14.6 ActiveX Automation .............................................................................759
Excel as Client (Controlling External Programs) ..........................................761
Excel as Server (Control by an External Program) .......................................767
New Objects for Excel (ClipBoard Example) ................................................772
Object Linking and Embedding (OLE) .........................................................775
Automation and Visual Basic .NET ...............................................................780
Launching and Controlling Programs Without ActiveX ..............................786
Syntax Summary .............................................................................................789

Part Four
Reference .....................................................................................................791

Chapter 15 Object Reference ............................................................793


15.1 Object Hierarchy ...................................................................................794
15.2 Alphabetical Reference ......................................................................801

Appendix Excel 2003 Beta 2 .........................................................863

References........................................................................................................879

Index ....................................................................................................................881

xii

You might also like