Introduction |
|
1 | (6) |
|
|
1 | (4) |
|
Typographical conventions |
|
|
2 | (1) |
|
|
3 | (2) |
|
|
5 | (1) |
|
|
5 | (1) |
|
|
6 | (1) |
|
|
6 | (1) |
Part 1: Starting Excel VBA Programming |
|
7 | (22) |
|
Chapter 1 Getting to Know VBA |
|
|
9 | (8) |
|
|
9 | (1) |
|
|
10 | (2) |
|
Inserting a bunch of text |
|
|
11 | (1) |
|
Automating a task you perform frequently |
|
|
11 | (1) |
|
Automating repetitive operations |
|
|
11 | (1) |
|
Creating a custom command |
|
|
11 | (1) |
|
|
12 | (1) |
|
Developing new worksheet functions |
|
|
12 | (1) |
|
Creating custom add-ins for Excel |
|
|
12 | (1) |
|
Getting the Most from VBA |
|
|
12 | (2) |
|
Knowing what VBA does best |
|
|
12 | (1) |
|
Recognizing the disadvantages of using VBA |
|
|
13 | (1) |
|
Understanding VBA Concepts |
|
|
14 | (2) |
|
Ensuring Excel Compatibility |
|
|
16 | (1) |
|
Chapter 2 Building Simple Macros |
|
|
17 | (12) |
|
Displaying the Developer Tab |
|
|
17 | (1) |
|
|
18 | (1) |
|
Preparing the Environment |
|
|
18 | (1) |
|
|
19 | (2) |
|
|
21 | (1) |
|
Viewing a Macro in the Visual Basic Editor |
|
|
22 | (2) |
|
|
24 | (1) |
|
Saving Workbooks That Contain Macros |
|
|
25 | (1) |
|
Understanding Macro Security |
|
|
25 | (4) |
Part 2: Employing VBA With Excel |
|
29 | (68) |
|
Chapter 3 Working in the Visual Basic Editor |
|
|
31 | (20) |
|
Getting to Know the Visual Basic Editor |
|
|
31 | (3) |
|
|
32 | (1) |
|
|
32 | (2) |
|
Working with the Project Explorer |
|
|
34 | (2) |
|
|
35 | (1) |
|
|
36 | (8) |
|
Minimizing and maximizing windows |
|
|
36 | (1) |
|
Looking at the parts of a module |
|
|
37 | (1) |
|
Getting VBA code into a module |
|
|
38 | (1) |
|
|
38 | (3) |
|
|
41 | (3) |
|
|
44 | (1) |
|
|
44 | (7) |
|
|
44 | (3) |
|
Using the Editor Format tab |
|
|
47 | (1) |
|
|
48 | (1) |
|
|
48 | (3) |
|
Chapter 4 Introducing the Excel Object Model |
|
|
51 | (16) |
|
Working with the Excel Object Model |
|
|
51 | (6) |
|
Understanding the object hierarchy |
|
|
52 | (1) |
|
|
53 | (4) |
|
Diving into Object Properties and Methods |
|
|
57 | (5) |
|
Setting object properties |
|
|
58 | (2) |
|
Taking action with object methods |
|
|
60 | (1) |
|
Triggering actions with object events |
|
|
61 | (1) |
|
Finding Out More from VBA Resources |
|
|
62 | (5) |
|
|
62 | (1) |
|
|
63 | (1) |
|
Automatically listing properties and methods |
|
|
64 | (3) |
|
Chapter 5 VBA Sub and Function Procedures |
|
|
67 | (16) |
|
Understanding Subs versus Functions |
|
|
67 | (2) |
|
Looking at Sub procedures |
|
|
68 | (1) |
|
Looking at Function procedures |
|
|
68 | (1) |
|
Naming Subs and Functions |
|
|
69 | (1) |
|
|
70 | (8) |
|
Executing the Sub procedure directly |
|
|
72 | (1) |
|
Executing the procedure from the Macro dialog box |
|
|
73 | (1) |
|
Executing a macro by using a shortcut key |
|
|
74 | (1) |
|
Executing the procedure from a button or shape |
|
|
75 | (2) |
|
Executing the procedure from another procedure |
|
|
77 | (1) |
|
Executing the procedure from the Immediate window |
|
|
77 | (1) |
|
Executing Function Procedures |
|
|
78 | (5) |
|
Calling the function from a Sub procedure |
|
|
78 | (1) |
|
Calling a function from the Immediate window |
|
|
79 | (1) |
|
Calling a function from a worksheet formula |
|
|
80 | (3) |
|
Chapter 6 Using the Excel Macro Recorder |
|
|
83 | (14) |
|
|
84 | (1) |
|
|
85 | (1) |
|
Choosing Between Relative and Absolute Mode |
|
|
86 | (5) |
|
Recording in absolute mode |
|
|
86 | (1) |
|
Recording in relative mode |
|
|
87 | (2) |
|
Watching the Macro Recorder in Action |
|
|
89 | (2) |
|
Specifying Recording Options for Your Macro |
|
|
91 | (1) |
|
Streamlining Code Generated by the Macro Recorder |
|
|
92 | (5) |
Part 3: Programming Concepts |
|
97 | (146) |
|
Chapter 7 Essential VBA Language Elements |
|
|
99 | (20) |
|
Using Comments in Your VBA Code |
|
|
99 | (2) |
|
Using Variables, Constants, and Data Types |
|
|
101 | (12) |
|
|
101 | (2) |
|
What are VBA's data types? |
|
|
103 | (1) |
|
Declaring and scoping variables |
|
|
103 | (7) |
|
|
110 | (1) |
|
|
110 | (1) |
|
|
111 | (1) |
|
|
112 | (1) |
|
Using Assignment Statements |
|
|
113 | (3) |
|
Assignment statement examples |
|
|
113 | (1) |
|
|
114 | (1) |
|
|
114 | (2) |
|
|
116 | (2) |
|
|
116 | (1) |
|
|
117 | (1) |
|
|
117 | (1) |
|
|
118 | (1) |
|
Chapter 8 Working with Range Objects |
|
|
119 | (16) |
|
Referring to Range Objects |
|
|
119 | (2) |
|
Referring to a Range Using Properties |
|
|
121 | (3) |
|
|
122 | (1) |
|
|
122 | (1) |
|
|
123 | (1) |
|
Working with Range Object Properties |
|
|
124 | (7) |
|
|
124 | (1) |
|
|
125 | (1) |
|
|
126 | (1) |
|
The Column and Row properties |
|
|
126 | (1) |
|
|
126 | (1) |
|
|
127 | (1) |
|
|
128 | (1) |
|
|
128 | (2) |
|
|
130 | (1) |
|
The NumberFormat property |
|
|
131 | (1) |
|
Taking Action with Range Object Methods |
|
|
131 | (4) |
|
|
132 | (1) |
|
The Copy and Paste methods |
|
|
132 | (1) |
|
|
133 | (1) |
|
|
133 | (2) |
|
Chapter 9 Using VBA and Worksheet Functions |
|
|
135 | (16) |
|
|
135 | (1) |
|
Using Built-In VBA Functions |
|
|
136 | (8) |
|
Working with dates and times |
|
|
136 | (2) |
|
|
138 | (1) |
|
Identifying objects and data |
|
|
139 | (1) |
|
VBA functions that do more than return a value |
|
|
140 | (1) |
|
Discovering VBA functions |
|
|
141 | (3) |
|
Using Worksheet Functions in VBA |
|
|
144 | (4) |
|
Worksheet function examples |
|
|
144 | (3) |
|
Entering worksheet functions |
|
|
147 | (1) |
|
More about using worksheet functions |
|
|
148 | (1) |
|
|
148 | (3) |
|
Chapter 10 Controlling Program Flow and Making Decisions |
|
|
151 | (20) |
|
Going with the Flow, Dude |
|
|
151 | (1) |
|
|
152 | (2) |
|
|
154 | (8) |
|
|
154 | (4) |
|
The Select Case structure |
|
|
158 | (4) |
|
Knocking Your Code for a Loop |
|
|
162 | (6) |
|
|
162 | (5) |
|
|
167 | (1) |
|
|
168 | (1) |
|
Using For Each-Next Loops with Collections |
|
|
168 | (3) |
|
Chapter 11 Automatic Procedures and Events |
|
|
171 | (22) |
|
Preparing for the Big Event |
|
|
171 | (3) |
|
Learning when to use event procedures |
|
|
173 | (1) |
|
Programming event-handler procedures |
|
|
174 | (1) |
|
Knowing Where to Put the Event Code |
|
|
174 | (1) |
|
Writing an Event-Handler Procedure |
|
|
175 | (2) |
|
Triggering Workbook Events |
|
|
177 | (4) |
|
The Open event fora workbook |
|
|
177 | (2) |
|
The BeforeClose event for a workbook |
|
|
179 | (1) |
|
The BeforeSave event for a workbook |
|
|
180 | (1) |
|
|
181 | (4) |
|
Activate and deactivate events in a sheet |
|
|
181 | (1) |
|
Activate and deactivate events in a workbook |
|
|
182 | (2) |
|
Workbook activation events |
|
|
184 | (1) |
|
Programming Worksheet-Related Events |
|
|
185 | (3) |
|
The BeforeDoubleClick event |
|
|
185 | (1) |
|
The BeforeRightClick event |
|
|
185 | (1) |
|
|
186 | (2) |
|
Understanding Events Not Associated with Objects |
|
|
188 | (5) |
|
|
188 | (2) |
|
|
190 | (3) |
|
Chapter 12 Error-Handling Techniques |
|
|
193 | (12) |
|
|
193 | (1) |
|
An Erroneous Macro Example |
|
|
194 | (4) |
|
|
195 | (1) |
|
|
196 | (2) |
|
|
198 | (1) |
|
Alternate Ways of Handling Errors |
|
|
198 | (2) |
|
Revisiting the EnterSquareRoot procedure |
|
|
198 | (2) |
|
Trapping errors with the On Error statement |
|
|
200 | (1) |
|
Handling Errors: The Details |
|
|
200 | (3) |
|
|
200 | (2) |
|
Error handling in a nutshell |
|
|
202 | (1) |
|
|
203 | (2) |
|
Chapter 13 Bug Extermination Techniques |
|
|
205 | (14) |
|
|
205 | (2) |
|
|
207 | (1) |
|
|
208 | (2) |
|
|
208 | (1) |
|
Using the MsgBox function |
|
|
208 | (2) |
|
Inserting Debug.Print statements |
|
|
210 | (1) |
|
|
210 | (7) |
|
Using the Debugger's Tools |
|
|
211 | (1) |
|
Setting breakpoints in your code |
|
|
211 | (3) |
|
|
214 | (2) |
|
|
216 | (1) |
|
|
217 | (2) |
|
Chapter 14 VBA Programming Examples |
|
|
219 | (24) |
|
|
220 | (10) |
|
|
220 | (1) |
|
Copying a variable-size range |
|
|
221 | (2) |
|
Selecting to the end of a row or column |
|
|
223 | (1) |
|
Selecting a row or column |
|
|
224 | (1) |
|
|
224 | (1) |
|
Looping through a range efficiently |
|
|
225 | (2) |
|
Looping through a range efficiently (Part II) |
|
|
227 | (1) |
|
Prompting for a cell value |
|
|
227 | (1) |
|
Determining the selection type |
|
|
228 | (1) |
|
Identifying a multiple selection |
|
|
229 | (1) |
|
|
230 | (1) |
|
Changing Boolean settings |
|
|
230 | (1) |
|
Changing non-Boolean settings |
|
|
231 | (1) |
|
|
231 | (6) |
|
AddChart versus AddChart2 |
|
|
232 | (2) |
|
|
234 | (1) |
|
Looping through the ChartObjects collection |
|
|
234 | (1) |
|
Modifying chart properties |
|
|
235 | (1) |
|
Applying chart formatting |
|
|
235 | (2) |
|
|
237 | (8) |
|
Turning off screen updating |
|
|
237 | (1) |
|
Turning off automatic calculation |
|
|
238 | (1) |
|
Eliminating those pesky alert messages |
|
|
239 | (1) |
|
Simplifying object references |
|
|
240 | (1) |
|
|
241 | (1) |
|
Using the With-End With structure |
|
|
242 | (1) |
Part 4: Communicating With Your Users |
|
243 | (100) |
|
Chapter 15 Simple Dialog Boxes |
|
|
245 | (18) |
|
Interacting with the User in VBA |
|
|
246 | (1) |
|
Displaying Messages with the MsgBox Function |
|
|
247 | (5) |
|
Displaying a simple message box |
|
|
247 | (1) |
|
Getting a response from a message box |
|
|
248 | (2) |
|
Customizing message boxes |
|
|
250 | (2) |
|
Getting Data with an Input Box |
|
|
252 | (4) |
|
Understanding the InputBox syntax |
|
|
253 | (1) |
|
Using the InputBox function |
|
|
253 | (2) |
|
Using the InputBox method |
|
|
255 | (1) |
|
Allowing the User to Select a File or Folder |
|
|
256 | (4) |
|
Constructing a GetOpenFilename statement |
|
|
256 | (1) |
|
Selecting a file with GetOpenFilename |
|
|
256 | (3) |
|
Picking a file with GetSaveAsFilename |
|
|
259 | (1) |
|
|
259 | (1) |
|
Displaying Excel's Built-In Dialog Boxes |
|
|
260 | (3) |
|
Chapter 16 UserForm Basics |
|
|
263 | (18) |
|
Knowing When to Use a UserForm |
|
|
263 | (2) |
|
Creating UserForms: An Overview |
|
|
265 | (1) |
|
|
266 | (5) |
|
|
266 | (1) |
|
Adding controls to a UserForm |
|
|
267 | (1) |
|
Changing properties for a UserForm control |
|
|
268 | (1) |
|
Viewing the UserForm Code pane |
|
|
269 | (1) |
|
|
269 | (1) |
|
Using information from a UserForm |
|
|
270 | (1) |
|
|
271 | (10) |
|
|
271 | (1) |
|
Adding the CommandButtons |
|
|
272 | (1) |
|
|
272 | (3) |
|
Adding event-handler procedures |
|
|
275 | (2) |
|
Creating a macro to display the dialog box |
|
|
277 | (1) |
|
Making the macro available |
|
|
277 | (1) |
|
|
278 | (3) |
|
Chapter 17 Using UserForm Controls |
|
|
281 | (20) |
|
Getting Started with Dialog Box Controls |
|
|
281 | (4) |
|
|
282 | (1) |
|
Introducing control properties |
|
|
283 | (2) |
|
Learning Dialog Box Controls Details |
|
|
285 | (11) |
|
|
285 | (1) |
|
|
286 | (1) |
|
|
287 | (1) |
|
|
288 | (1) |
|
|
288 | (1) |
|
|
289 | (1) |
|
|
289 | (2) |
|
|
291 | (1) |
|
|
292 | (1) |
|
|
292 | (1) |
|
|
293 | (1) |
|
|
294 | (1) |
|
|
295 | (1) |
|
|
295 | (1) |
|
|
296 | (1) |
|
Working with Dialog Box Controls |
|
|
296 | (4) |
|
Moving and resizing controls |
|
|
297 | (1) |
|
Aligning and spacing controls |
|
|
297 | (1) |
|
Accommodating keyboard users |
|
|
297 | (3) |
|
|
300 | (1) |
|
|
300 | (1) |
|
Chapter 18 UserForm Techniques and Tricks |
|
|
301 | (28) |
|
|
302 | (1) |
|
|
302 | (7) |
|
|
302 | (3) |
|
Writing code to display the dialog box |
|
|
305 | (1) |
|
Making the macro available |
|
|
305 | (1) |
|
Trying out your dialog box |
|
|
306 | (1) |
|
Adding event-handler procedures |
|
|
307 | (2) |
|
|
309 | (1) |
|
|
309 | (1) |
|
A ListBox Control Example |
|
|
309 | (5) |
|
Filling a ListBox Control |
|
|
310 | (2) |
|
Determining the selected item |
|
|
312 | (1) |
|
Determining multiple selections |
|
|
313 | (1) |
|
|
314 | (2) |
|
Using Multiple Sets of Option Buttons |
|
|
316 | (1) |
|
Using a Spin Button and a Text Box |
|
|
317 | (2) |
|
Using a UserForm as a Progress Indicator |
|
|
319 | (4) |
|
Creating the progress-indicator dialog box |
|
|
320 | (1) |
|
|
321 | (1) |
|
|
322 | (1) |
|
Creating a Modeless Tabbed Dialog Box |
|
|
323 | (2) |
|
Displaying a Chart in a UserForm |
|
|
325 | (1) |
|
|
326 | (3) |
|
Chapter 19 Accessing Your Macros through the User Interface |
|
|
329 | (14) |
|
|
329 | (8) |
|
Customizing the Ribbon manually |
|
|
330 | (2) |
|
Adding a macro to the Ribbon |
|
|
332 | (1) |
|
Customizing the Ribbon with XML |
|
|
333 | (4) |
|
Customizing the Excel UI with VBA |
|
|
337 | (8) |
|
Adding commands to the Add-ins Ribbon tab |
|
|
338 | (1) |
|
Adding a new item to the Cell shortcut menu |
|
|
339 | (1) |
|
Adding customizations automatically |
|
|
340 | (1) |
|
Understanding shortcut menus and the single document interface |
|
|
341 | (2) |
Part 5: Putting It All Together |
|
343 | (34) |
|
Chapter 20 Creating Worksheet Functions |
|
|
345 | (20) |
|
Create Custom Functions to Simplify Your Work |
|
|
345 | (2) |
|
Understanding VBA Function Basics |
|
|
347 | (1) |
|
|
347 | (1) |
|
Working with Function Arguments |
|
|
348 | (8) |
|
A function with no argument |
|
|
349 | (1) |
|
A function with one argument |
|
|
349 | (2) |
|
A function with two arguments |
|
|
351 | (1) |
|
A function with a range argument |
|
|
352 | (2) |
|
A function with an optional argument |
|
|
354 | (2) |
|
Introducing Wrapper Functions |
|
|
356 | (3) |
|
The NumberFormat function |
|
|
356 | (1) |
|
The ExtractElement function |
|
|
357 | (1) |
|
|
358 | (1) |
|
|
358 | (1) |
|
Working with Functions That Return an Array |
|
|
359 | (3) |
|
Returning an array of month names |
|
|
359 | (1) |
|
|
360 | (2) |
|
Using the Insert Function Dialog Box |
|
|
362 | (3) |
|
Displaying the function's description |
|
|
362 | (2) |
|
Adding argument descriptions |
|
|
364 | (1) |
|
Chapter 21 Creating Excel Add-Ins |
|
|
365 | (12) |
|
|
365 | (1) |
|
Reasons to Create Add-Ins |
|
|
366 | (1) |
|
|
367 | (1) |
|
Understanding Add-In Basics |
|
|
368 | (1) |
|
Looking at an Add-In Example |
|
|
369 | (10) |
|
|
369 | (3) |
|
|
372 | (1) |
|
Adding descriptive information |
|
|
372 | (1) |
|
|
373 | (1) |
|
|
374 | (1) |
|
|
374 | (1) |
|
|
375 | (1) |
|
|
375 | (2) |
Part 6: The Part Of Tens |
|
377 | (24) |
|
Chapter 22 Ten Handy Visual Basic Editor Tips |
|
|
379 | (10) |
|
|
380 | (1) |
|
Copying Multiple Lines of Code at Once |
|
|
381 | (1) |
|
Jumping between Modules and Procedures |
|
|
382 | (1) |
|
Teleporting to Your Functions |
|
|
382 | (1) |
|
Staying in the Right Procedure |
|
|
383 | (1) |
|
Stepping through Your Code |
|
|
383 | (1) |
|
Stepping to a Specific Line in Your Code |
|
|
384 | (1) |
|
Stopping Your Code at a Predefined Point |
|
|
385 | (1) |
|
Seeing the Beginning and End of Variable Values |
|
|
386 | (1) |
|
Turning Off Auto Syntax Check |
|
|
387 | (2) |
|
Chapter 23 Resources for VBA Help |
|
|
389 | (6) |
|
Letting Excel Write Code for You |
|
|
390 | (1) |
|
Referencing the Help System |
|
|
390 | (1) |
|
Pilfering Code from the Internet |
|
|
390 | (1) |
|
|
391 | (1) |
|
|
392 | (1) |
|
Mining YouTube for Video Training |
|
|
393 | (1) |
|
Attending Live and Online Training Classes |
|
|
393 | (1) |
|
Learning from the Microsoft Office Dev Center |
|
|
393 | (1) |
|
Dissecting the Other Excel Files in Your Organization |
|
|
394 | (1) |
|
Asking Your Local Excel Guru |
|
|
394 | (1) |
|
Chapter 24 Ten VBA Do's and Don'ts |
|
|
395 | (6) |
|
|
395 | (1) |
|
Don't Confuse Passwords with Security |
|
|
396 | (1) |
|
|
396 | (1) |
|
Don't Put Everything in One Procedure |
|
|
397 | (1) |
|
Do Consider Other Software |
|
|
397 | (1) |
|
Don't Assume That Everyone Enables Macros |
|
|
397 | (1) |
|
Do Get in the Habit of Experimenting |
|
|
398 | (1) |
|
Don't Assume That Your Code Will Work with Other Excel Versions |
|
|
398 | (1) |
|
Do Keep Your Users in Mind |
|
|
399 | (1) |
|
Don't Forget about Backups |
|
|
399 | (2) |
Index |
|
401 | |