没有合适的资源?快使用搜索试试~ 我知道了~
首页Office 2013 VBA全攻略:打造定制应用与高效自动化
Office 2013 VBA全攻略:打造定制应用与高效自动化
![](https://csdnimg.cn/release/wenkucmsfe/public/img/star.98a08eaa.png)
"《Mastering VBA for Microsoft Office 2013》是一本专为想要利用Visual Basic for Applications (VBA)提升Microsoft Office 2013工作效率的读者打造的全面指南。这本书不仅适合初学者,也适合经验丰富的VB开发者,因为它提供了从基础到进阶的系统性教程。 书中首先会引导读者了解和掌握VBA的基础知识,包括如何录制宏、入门VBA编程,以及如何有效地运用循环、函数等核心概念。作者还会深入讲解如何利用消息框、输入框和对话框进行用户交互,使程序更具人性化。此外,书中的内容涵盖了XML文件、ActiveX控件、开发者选项卡、内容控制、Add-ins(插件)和嵌入式宏等高级特性,帮助读者更好地理解并应用这些技术。 为了确保理论知识与实践相结合,书中还包含了多个真实世界项目示例,涉及Word、Excel、Outlook、PowerPoint和Access等Office 2013应用。通过这些实例,读者可以学习如何在各个办公软件中编写和应用VBA代码,实现自动化任务,从而显著提高生产力。 本书的结构清晰,语言易懂,对于希望精通VBA并在Microsoft Office 2013环境中开发定制应用程序的专业人士来说,是不可或缺的参考资源。无论是初出茅庐的新手还是寻求进一步提升的开发者,都能从中获得宝贵的知识和技能。版权信息表明,未经许可,任何形式的复制或传播都必须遵循美国1976年版权法的相关规定。"
资源详情
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/9563905/bg10.jpg)
XIV
|
CONTENTS
ftoc.indd 07:28:45:PM 07/26/2013 Page xiv
Working with Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Working with an Object in a Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Adding an Object to a Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Finding the Objects You Need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Using the Macro Recorder to Add Code for the Objects You Need . . . . . . . . . . . . . 178
Using the Object Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Using Help to nd the Object You Need. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Using the Auto List Members Feature. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Using Object Variables to Represent Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Team Programming and OOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Part
3 •
Making Decisions and Using Loops and Functions . . . . . . . . . . . . . . . . . . . 197
Chapter 9 • Using Built-in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
What Is a Function? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Passing Arguments to a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Using Functions to Convert Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Using the Asc Function to Return a Character Code . . . . . . . . . . . . . . . . . . . . . . . . . 205
Using the Val Function to Extract a Number from the Start of a String . . . . . . . . . 206
Using the Str Function to Convert a Number into a String . . . . . . . . . . . . . . . . . . . . 207
Using the Format Function to Format an Expression . . . . . . . . . . . . . . . . . . . . . . . . . 208
Using the Chr Function and Constants to Enter Special Characters in a String. . . 212
Using Functions to Manipulate Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Using the Left, Right, and Mid Functions to Return Part of a String. . . . . . . . . . . . . 215
Using InStr and InStrRev to Find a String within Another String . . . . . . . . . . . . . . 219
Using LTrim, RTrim, and Trim to Remove Spaces from a String . . . . . . . . . . . . . . . . 221
Using Len to Check the Length of a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Using StrConv, LCase, and UCase to Change the Case of a String. . . . . . . . . . . . . . . 224
Using the StrComp Function to Compare Apples to Apples . . . . . . . . . . . . . . . . . . . 225
Using VBA’s Mathematical Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Using VBA’s Date and Time Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Using the DatePart Function to Parse Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Using the DateDiff Function to Figure Out a Time Interval. . . . . . . . . . . . . . . . . . . . 229
Using the DateAdd Function to Add or Subtract Time from a Date . . . . . . . . . . . . . 230
Using File-Management Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Using the Dir Function to Check Whether a File Exists . . . . . . . . . . . . . . . . . . . . . . . 230
Returning the Current Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Chapter 10 • Creating Your Own Functions . . . . . . . . . . . . . . . . . . . . . . . 235
Components of a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Creating a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Starting a Function Manually. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Starting a Function by Using the Add Procedure Dialog Box. . . . . . . . . . . . . . . . . . 239
![](https://csdnimg.cn/release/download_crawler_static/9563905/bg11.jpg)
CONTENTS
|
XV
ftoc.indd 07:28:45:PM 07/26/2013 Page xv
Passing Arguments to a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Declaring the Data Types of Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Specifying an Optional Argument . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Controlling the Scope of a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Examples of Functions for Any VBA-Enabled Of ce Application. . . . . . . . . . . . . . . . . 241
How Functions Return Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Returning Text Data from a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Creating a Function for Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Creating a Function for Excel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Creating a Function for PowerPoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Creating a Function for Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Chapter 11 • Making Decisions in Your Code . . . . . . . . . . . . . . . . . . . . . . 255
How Do You Compare Things in VBA? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Testing Multiple Conditions by Using Logical Operators. . . . . . . . . . . . . . . . . . . . . . . . 257
If Blocks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
If… Then . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
If… Then… Else Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
If… Then… ElseIf… Else Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Creating Loops with If and GoTo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Nesting If Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Select Case Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
When Order Matters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Chapter 12 • Using Loops to Repeat Actions . . . . . . . . . . . . . . . . . . . . . . . 277
When Should You Use a Loop? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Understanding the Basics of Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Using For… Loops for Fixed Repetitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
For… Next Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
For Each… Next Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Using an Exit For Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Using Do… Loops for Variable Numbers of Repetitions . . . . . . . . . . . . . . . . . . . . . . . . . 288
Do While… Loop Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Do… Loop While Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Do Until… Loop Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Do… Loop Until Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Using an Exit Do Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Is the Exit Do Statement Bad Practice?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
While… Wend Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Nesting Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Avoiding In nite Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
![](https://csdnimg.cn/release/download_crawler_static/9563905/bg12.jpg)
XVI
|
CONTENTS
ftoc.indd 07:28:45:PM 07/26/2013 Page xvi
Part 4
•
Using Message Boxes, Input Boxes, and Dialog Boxes . . . . . . . . . . . . . . . . 305
Chapter 13 • Getting User Input with Message Boxes and Input Boxes 307
Opening a Procedure to Work On. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Displaying Status Bar Messages in Word and Excel . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Message Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
The Pros and Cons of Message Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Message-Box Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Displaying a Simple Message Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Displaying a Multiline Message Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Choosing Buttons for a Message Box. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Choosing an Icon for a Message Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Setting a Default Button for a Message Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Controlling the Modality of a Message Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Specifying a Title for a Message Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Title Bars Can Provide Useful Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Adding a Help Button to a Message Box. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Specifying a Help File for a Message Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Using Some Arguments without Others. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Retrieving a Value from a Message Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Input Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Input-Box Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Retrieving Input from an Input Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Forms: When Message Boxes and Input Boxes Won’t Suf ce . . . . . . . . . . . . . . . . . . . . . 326
The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Chapter 14 • Creating Simple Custom Dialog Boxes . . . . . . . . . . . . . . . . . 329
When Should You Use a Custom Dialog Box?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Creating a Custom Dialog Box. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
Designing a Dialog Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Inserting a User Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Renaming a User Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Adding Controls to the User Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Grouping Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Renaming Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Moving a Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
Changing the Caption on a Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Key Properties of the Toolbox Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Working with Groups of Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Aligning Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Placing Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Adjusting the Tab Order of a Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
![](https://csdnimg.cn/release/download_crawler_static/9563905/bg13.jpg)
CONTENTS
|
XVII
ftoc.indd 07:28:45:PM 07/26/2013 Page xvii
Linking a Form to a Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Loading and Unloading a Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Displaying and Hiding a Form. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Setting a Default Command Button. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Retrieving the User’s Choices from a Dialog Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Returning a String from a Text Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Returning a Value from an Option Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Returning a Value from a Check Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Returning a Value from a List Box. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Returning a Value from a Combo Box. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
Examples of Connecting Forms to Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
Word Example: The Move-Paragraph Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
General Example: Opening a File from a List Box. . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Creating the Code for the User Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
Using an Application’s Built-in Dialog Boxes from VBA. . . . . . . . . . . . . . . . . . . . . . . . . 391
Displaying a Built-in Dialog Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Setting and Restoring Options in a Built-in Dialog Box. . . . . . . . . . . . . . . . . . . . . . . 395
Which Button Did the User Choose in a Dialog Box?. . . . . . . . . . . . . . . . . . . . . . . . . 395
Specifying a Time-Out for a Dialog Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Chapter 15 • Creating Complex Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Creating and Working with Complex Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
Updating a Dialog Box to Re ect the User’s Choices . . . . . . . . . . . . . . . . . . . . . . . . . 400
Revealing a Hidden Part of a Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
Tracking a Procedure in a Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Using Multipage Dialog Boxes and Tab Strip Controls . . . . . . . . . . . . . . . . . . . . . . . 408
Creating a Modeless Dialog Box. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Specifying a Form’s Location on Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
Using Events to Control Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
Events Unique to the UserForm Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
Events That Apply to Both UserForms and Container Controls . . . . . . . . . . . . . . . . 428
Events That Apply to Many or Most Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
Events That Apply Only to a Few Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
Part
5 •
Creating Eff ective Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Chapter 16 • Building Modular Code and Using Classes . . . . . . . . . . . . . 451
Creating Modular Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
What Is Modular Code? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Advantages of Using Modular Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
How to Approach Creating Modular Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Arranging Your Code in Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
![](https://csdnimg.cn/release/download_crawler_static/9563905/bg14.jpg)
XVIII
|
CONTENTS
ftoc.indd 07:28:45:PM 07/26/2013 Page xviii
Calling a Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
Making Logical Improvements to Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
Making Visual Improvements to Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Creating and Using Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
What Can You Do with Class Modules? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
A Brief Overview of Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Planning Your Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Creating a Class Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
Naming the Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
Setting the Instancing Property. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
Declaring Variables and Constants for the Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
Adding Properties to the Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
Adding Methods to a Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
Using Your Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
Chapter 17 • Debugging Your Code and Handling Errors . . . . . . . . . . . . . 481
Principles of Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
The Different Types of Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
Language Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
Compile Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
Runtime Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
Program Logic Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
VBA’s Debugging Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
Break Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
The Step Over and Step Out Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
The Locals Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
The Watch Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
The Immediate Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
The Call Stack Dialog Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
Dealing with In nite Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Dealing with Runtime Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
When Should You Write an Error Handler?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Trapping an Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Disabling an Error Trap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
Resuming after an Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
Getting a Description of an Error. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
Raising Your Own Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
Suppressing Alerts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
Handling User Interrupts in Word, Excel, and Project . . . . . . . . . . . . . . . . . . . . . . . . . . 505
Disabling User Input While a Procedure Is Running. . . . . . . . . . . . . . . . . . . . . . . . . 506
Disabling User Input While Part of a Procedure Is Running . . . . . . . . . . . . . . . . . . 506
Documenting Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
The Bottom Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
剩余961页未读,继续阅读
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://profile-avatar.csdnimg.cn/e52a7af1356d46f7b0ef8d084a0944ef_dzweather.jpg!1)
dzweather
- 粉丝: 1055
- 资源: 121
上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![](https://csdnimg.cn/release/wenkucmsfe/public/img/voice.245cc511.png)
会员权益专享
最新资源
- 谷歌文件系统下的实用网络编码技术在分布式存储中的应用
- 跨国媒体对南亚农村社会的影响:以斯里兰卡案例的社会学分析
- RFM2g接口驱动操作手册:API与命令行指南
- 基于裸手的大数据自然人机交互关键算法研究
- ABAQUS下无人机机翼有限元分析与局部设计研究
- TCL基础教程:语法、变量与操作详解
- FPGA与数字前端面试题集锦:流程、设计与Verilog应用
- 2022全球互联网技术人才前瞻:元宇宙驱动下的创新与挑战
- 碳排放权交易实战手册(第二版):设计与实施指南
- 2022新经济新职业洞察:科技驱动下的百景变革
- 红外与可见光人脸融合识别技术探究
- NXP88W8977:2.4/5 GHz 双频 Wi-Fi4 + Bluetooth 5.2 合体芯片
- NXP88W8987:集成2.4/5GHz Wi-Fi 5与蓝牙5.2的单芯片解决方案
- TPA3116D2DADR: 单声道数字放大器驱动高达50W功率
- TPA3255-Q1:315W车载A/D类音频放大器,高保真、宽频设计
- 42V 输入 5A 降压稳压器 TPS54540B-Q1 的特点和应用
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![](https://csdnimg.cn/release/wenkucmsfe/public/img/green-success.6a4acb44.png)