| Introduction |
|
1 | (10) |
|
|
|
1 | (1) |
|
So You Want to Be a Programmer |
|
|
2 | (1) |
|
|
|
3 | (1) |
|
|
|
3 | (1) |
|
Obligatory Typographical Conventions Section |
|
|
4 | (1) |
|
Check Your Security Settings |
|
|
5 | (1) |
|
How This Book Is Organized |
|
|
6 | (2) |
|
|
|
6 | (1) |
|
Part II How VBA Works with Excel |
|
|
6 | (1) |
|
Part III Programming Concepts |
|
|
7 | (1) |
|
Part IV Communicating with Your Users |
|
|
7 | (1) |
|
Part V Putting It All Together |
|
|
7 | (1) |
|
|
|
7 | (1) |
|
|
|
7 | (1) |
|
|
|
8 | (1) |
|
|
|
8 | (1) |
|
|
|
9 | (2) |
|
|
|
11 | (24) |
|
|
|
13 | (10) |
|
|
|
13 | (1) |
|
What Can You Do with VBA? |
|
|
14 | (2) |
|
Inserting a bunch of text |
|
|
15 | (1) |
|
Automating a task you perform frequently |
|
|
15 | (1) |
|
Automating repetitive operations |
|
|
15 | (1) |
|
Creating a custom command |
|
|
15 | (1) |
|
|
|
16 | (1) |
|
Developing new worksheet functions |
|
|
16 | (1) |
|
Creating complete, macro-driven applications |
|
|
16 | (1) |
|
Creating custom add-ins for Excel |
|
|
16 | (1) |
|
Advantages and Disadvantages of VBA |
|
|
16 | (2) |
|
|
|
17 | (1) |
|
|
|
17 | (1) |
|
|
|
18 | (2) |
|
An Excursion into Versions |
|
|
20 | (3) |
|
Chapter 2 Jumping Right In |
|
|
23 | (12) |
|
|
|
23 | (2) |
|
|
|
24 | (1) |
|
|
|
24 | (1) |
|
|
|
25 | (1) |
|
|
|
25 | (1) |
|
|
|
26 | (1) |
|
|
|
27 | (1) |
|
|
|
27 | (3) |
|
|
|
30 | (1) |
|
Saving Workbooks That Contain Macros |
|
|
30 | (1) |
|
Understanding Macro Security |
|
|
31 | (2) |
|
More about the NameAndTime Macro |
|
|
33 | (2) |
|
Part II How VBA Works with Excel |
|
|
35 | (58) |
|
Chapter 3 Working in the Visual Basic Editor |
|
|
37 | (32) |
|
What Is the Visual Basic Editor? |
|
|
37 | (3) |
|
|
|
37 | (1) |
|
Understanding VBE components |
|
|
38 | (2) |
|
Working with the Project Window |
|
|
40 | (2) |
|
|
|
41 | (1) |
|
|
|
41 | (1) |
|
Exporting and importing objects |
|
|
42 | (1) |
|
Working with a Code Window |
|
|
42 | (7) |
|
Minimizing and maximizing windows |
|
|
42 | (2) |
|
|
|
44 | (1) |
|
Getting VBA code into a module |
|
|
44 | (1) |
|
|
|
45 | (2) |
|
|
|
47 | (2) |
|
|
|
49 | (1) |
|
Customizing the VBA Environment |
|
|
49 | (6) |
|
|
|
50 | (2) |
|
Using the Editor Format tab |
|
|
52 | (1) |
|
|
|
53 | (1) |
|
|
|
54 | (1) |
|
Chapter 4 Introducing the Excel Object Model |
|
|
55 | (3) |
|
|
|
56 | (1) |
|
Climbing the Object Hierarchy |
|
|
56 | (2) |
|
Wrapping Your Mind around Collections |
|
|
58 | (1) |
|
|
|
58 | (2) |
|
Navigating through the hierarchy |
|
|
59 | (1) |
|
Simplifying object references |
|
|
60 | (1) |
|
Diving into Object Properties and Methods |
|
|
60 | (4) |
|
|
|
62 | (1) |
|
|
|
63 | (1) |
|
|
|
64 | (1) |
|
|
|
64 | (5) |
|
|
|
65 | (1) |
|
|
|
66 | (1) |
|
Automatically listing properties and methods |
|
|
66 | (3) |
|
Chapter 5 VBA Sub and Function Procedures |
|
|
69 | (12) |
|
|
|
69 | (2) |
|
Looking at Sub procedures |
|
|
70 | (1) |
|
Looking at Function procedures |
|
|
70 | (1) |
|
Naming Subs and Functions |
|
|
71 | (1) |
|
|
|
71 | (7) |
|
Executing the Sub procedure directly |
|
|
73 | (1) |
|
Executing the procedure from the Macro dialog box |
|
|
74 | (1) |
|
Executing a macro by using a shortcut key |
|
|
75 | (1) |
|
Executing the procedure from a button or shape |
|
|
76 | (2) |
|
Executing the procedure from another procedure |
|
|
78 | (1) |
|
Executing Function procedures |
|
|
78 | (3) |
|
Calling the function from a Sub procedure |
|
|
79 | (1) |
|
Calling a function from a worksheet formula |
|
|
79 | (2) |
|
Chapter 6 Using the Excel Macro Recorder |
|
|
81 | (12) |
|
|
|
81 | (1) |
|
|
|
82 | (2) |
|
|
|
84 | (1) |
|
|
|
84 | (3) |
|
Recording in absolute mode |
|
|
84 | (1) |
|
Recording in relative mode |
|
|
85 | (2) |
|
|
|
87 | (1) |
|
|
|
88 | (2) |
|
|
|
88 | (1) |
|
|
|
89 | (1) |
|
|
|
89 | (1) |
|
|
|
89 | (1) |
|
|
|
90 | (3) |
|
Part III Programming Concepts |
|
|
93 | (140) |
|
Chapter 7 Essential VBA Language Elements |
|
|
95 | (20) |
|
Using Comments in Your VBA Code |
|
|
95 | (2) |
|
Using Variables, Constants, and Data Types |
|
|
97 | (12) |
|
|
|
97 | (1) |
|
What are VBA's data types? |
|
|
98 | (1) |
|
Declaring and scoping variables |
|
|
99 | (6) |
|
|
|
105 | (1) |
|
|
|
106 | (1) |
|
|
|
107 | (1) |
|
|
|
108 | (1) |
|
Using Assignment Statements |
|
|
109 | (3) |
|
Assignment statement examples |
|
|
109 | (1) |
|
|
|
109 | (1) |
|
|
|
110 | (2) |
|
|
|
112 | (2) |
|
|
|
112 | (1) |
|
|
|
113 | (1) |
|
|
|
113 | (1) |
|
|
|
114 | (1) |
|
Chapter 8 Working with Range Objects |
|
|
115 | (14) |
|
|
|
115 | (2) |
|
Other Ways to Refer to a Range |
|
|
117 | (2) |
|
|
|
117 | (1) |
|
|
|
118 | (1) |
|
Referring to entire columns and rows |
|
|
119 | (1) |
|
Some Useful Range Object Properties |
|
|
119 | (7) |
|
|
|
120 | (1) |
|
|
|
121 | (1) |
|
|
|
121 | (1) |
|
The Column and Row properties |
|
|
121 | (1) |
|
|
|
122 | (1) |
|
|
|
122 | (1) |
|
|
|
123 | (1) |
|
|
|
123 | (1) |
|
|
|
124 | (2) |
|
The NumberFormat property |
|
|
126 | (1) |
|
Some Useful Range Object Methods |
|
|
126 | (3) |
|
|
|
126 | (1) |
|
The Copy and Paste methods |
|
|
127 | (1) |
|
|
|
128 | (1) |
|
|
|
128 | (1) |
|
Chapter 9 Using VBA and Worksheet Functions |
|
|
129 | (12) |
|
|
|
129 | (1) |
|
Using Built-In VBA Functions |
|
|
130 | (5) |
|
|
|
130 | (2) |
|
VBA functions that do more than return a value |
|
|
132 | (1) |
|
Discovering VBA functions |
|
|
133 | (2) |
|
Using Worksheet Functions in VBA |
|
|
135 | (4) |
|
Worksheet function examples |
|
|
136 | (2) |
|
Entering worksheet functions |
|
|
138 | (1) |
|
More about Using Worksheet Functions |
|
|
139 | (1) |
|
|
|
139 | (2) |
|
Chapter 10 Controlling Program Flow and Making Decisions |
|
|
141 | (20) |
|
Going with the Flow, Dude |
|
|
141 | (1) |
|
|
|
142 | (9) |
|
|
|
143 | (1) |
|
|
|
143 | (5) |
|
The Select Case structure |
|
|
148 | (3) |
|
Knocking Your Code for a Loop |
|
|
151 | (7) |
|
|
|
152 | (4) |
|
|
|
156 | (1) |
|
|
|
157 | (1) |
|
Looping through a Collection |
|
|
158 | (3) |
|
Chapter 11 Automatic Procedures and Events |
|
|
161 | (22) |
|
Preparing for the Big Event |
|
|
161 | (3) |
|
|
|
163 | (1) |
|
Programming event-handler procedures |
|
|
164 | (1) |
|
Where Does the VBA Code Go? |
|
|
164 | (1) |
|
Writing an Event-Handler Procedure |
|
|
165 | (2) |
|
|
|
167 | (3) |
|
The Open event for a workbook |
|
|
167 | (2) |
|
The BeforeClose event for a workbook |
|
|
169 | (1) |
|
The BeforeSave event for a workbook |
|
|
169 | (1) |
|
Examples of Activation Events |
|
|
170 | (4) |
|
Activate and deactivate events in a sheet |
|
|
170 | (1) |
|
Activate and deactivate events in a workbook |
|
|
171 | (2) |
|
Workbook activation events |
|
|
173 | (1) |
|
Other Worksheet-Related Events |
|
|
174 | (3) |
|
The BeforeDoubleClick event |
|
|
174 | (1) |
|
The BeforeRightClick event |
|
|
174 | (1) |
|
|
|
175 | (2) |
|
Events Not Associated with Objects |
|
|
177 | (6) |
|
|
|
178 | (2) |
|
|
|
180 | (3) |
|
Chapter 12 Error-Handling Techniques |
|
|
183 | (14) |
|
|
|
183 | (1) |
|
|
|
184 | (4) |
|
The macro's not quite perfect |
|
|
185 | (1) |
|
The macro is still not perfect |
|
|
186 | (1) |
|
Is the macro perfect yet? |
|
|
186 | (1) |
|
|
|
187 | (1) |
|
Handling Errors Another Way |
|
|
188 | (2) |
|
Revisiting the EnterSquareRoot procedure |
|
|
188 | (1) |
|
About the On Error statement |
|
|
189 | (1) |
|
Handling Errors: The Details |
|
|
190 | (4) |
|
|
|
190 | (2) |
|
Error handling in a nutshell |
|
|
192 | (1) |
|
Knowing when to ignore errors |
|
|
192 | (1) |
|
Identifying specific errors |
|
|
193 | (1) |
|
|
|
194 | (3) |
|
Chapter 13 Bug Extermination Techniques |
|
|
197 | (14) |
|
|
|
197 | (1) |
|
|
|
198 | (1) |
|
|
|
199 | (3) |
|
|
|
199 | (1) |
|
Using the MsgBox function |
|
|
200 | (1) |
|
Inserting Debug Print statements |
|
|
201 | (1) |
|
|
|
202 | (1) |
|
|
|
202 | (6) |
|
Setting breakpoints in your code |
|
|
202 | (4) |
|
|
|
206 | (1) |
|
|
|
207 | (1) |
|
|
|
208 | (3) |
|
Chapter 14 VBA Programming Examples |
|
|
211 | (22) |
|
|
|
211 | (9) |
|
|
|
212 | (1) |
|
Copying a variable-sized range |
|
|
213 | (1) |
|
Selecting to the end of a row or column |
|
|
214 | (1) |
|
Selecting a row or column |
|
|
215 | (1) |
|
|
|
215 | (1) |
|
Looping through a range efficiently |
|
|
216 | (1) |
|
Looping through a range efficiently (Part II) |
|
|
217 | (1) |
|
Prompting for a cell value |
|
|
218 | (1) |
|
Determining the selection type |
|
|
219 | (1) |
|
Identifying a multiple selection |
|
|
219 | (1) |
|
|
|
220 | (2) |
|
Changing Boolean settings |
|
|
221 | (1) |
|
Changing non-Boolean settings |
|
|
221 | (1) |
|
|
|
222 | (5) |
|
|
|
224 | (1) |
|
Looping through the ChartObjects collection |
|
|
224 | (1) |
|
Modifying chart properties |
|
|
225 | (1) |
|
Applying chart formatting |
|
|
225 | (2) |
|
|
|
227 | (6) |
|
Turning off screen updating |
|
|
227 | (1) |
|
Turning off automatic calculation |
|
|
228 | (1) |
|
Eliminating those pesky alert messages |
|
|
228 | (1) |
|
Simplifying object references |
|
|
229 | (1) |
|
|
|
230 | (1) |
|
Using the With-End With structure |
|
|
231 | (2) |
|
Part IV Communicating with Your Users |
|
|
233 | (98) |
|
Chapter 15 Simple Dialog Boxes |
|
|
235 | (18) |
|
|
|
235 | (1) |
|
|
|
236 | (5) |
|
Getting a response from a message box |
|
|
237 | (1) |
|
Customizing message boxes |
|
|
238 | (3) |
|
|
|
241 | (3) |
|
|
|
241 | (1) |
|
|
|
242 | (2) |
|
The GetOpenFilename Method |
|
|
244 | (4) |
|
The syntax for the GetOpenFilename method |
|
|
244 | (1) |
|
A GetOpenFilename example |
|
|
245 | (2) |
|
|
|
247 | (1) |
|
The GetSaveAsFilename Method |
|
|
248 | (1) |
|
|
|
249 | (1) |
|
Displaying Excel's Built-in Dialog Boxes |
|
|
250 | (3) |
|
Chapter 16 UseForm Basics |
|
|
253 | (16) |
|
Knowing When to Use a UserForm |
|
|
253 | (1) |
|
Creating UserForms: An Overview |
|
|
254 | (1) |
|
|
|
255 | (5) |
|
|
|
255 | (1) |
|
Adding controls to a UserForm |
|
|
256 | (1) |
|
Changing properties for a UserForm control |
|
|
257 | (1) |
|
Viewing the UserForm Code window |
|
|
258 | (1) |
|
|
|
259 | (1) |
|
Using information from a UserForm |
|
|
259 | (1) |
|
|
|
260 | (9) |
|
|
|
260 | (1) |
|
Adding the CommandButtons |
|
|
261 | (1) |
|
|
|
262 | (1) |
|
Adding event-handler procedures |
|
|
263 | (2) |
|
Creating a macro to display the dialog box |
|
|
265 | (1) |
|
Making the macro available |
|
|
266 | (1) |
|
|
|
267 | (2) |
|
Chapter 17 Using UserForm Controls |
|
|
269 | (20) |
|
Getting Started with Dialog Box Controls |
|
|
269 | (4) |
|
|
|
269 | (1) |
|
Introducing control properties |
|
|
270 | (3) |
|
Dialog Box Controls: The Details |
|
|
273 | (11) |
|
|
|
274 | (1) |
|
|
|
274 | (1) |
|
|
|
275 | (1) |
|
|
|
276 | (1) |
|
|
|
276 | (1) |
|
|
|
277 | (1) |
|
|
|
278 | (1) |
|
|
|
279 | (1) |
|
|
|
279 | (1) |
|
|
|
280 | (1) |
|
|
|
281 | (1) |
|
|
|
282 | (1) |
|
|
|
282 | (1) |
|
|
|
282 | (1) |
|
|
|
283 | (1) |
|
Working with Dialog Box Controls |
|
|
284 | (3) |
|
Moving and resizing controls |
|
|
284 | (1) |
|
Aligning and spacing controls |
|
|
284 | (1) |
|
Accommodating keyboard users |
|
|
285 | (2) |
|
|
|
287 | (1) |
|
|
|
287 | (2) |
|
Chapter 18 UserForm Techniques and Tricks |
|
|
289 | (24) |
|
|
|
289 | (1) |
|
|
|
289 | (7) |
|
|
|
290 | (2) |
|
Writing code to display the dialog box |
|
|
292 | (1) |
|
Making the macro available |
|
|
292 | (1) |
|
Trying out your dialog box |
|
|
293 | (1) |
|
Adding event-handler procedures |
|
|
294 | (1) |
|
|
|
295 | (1) |
|
|
|
296 | (1) |
|
|
|
296 | (15) |
|
|
|
296 | (5) |
|
|
|
301 | (1) |
|
Using multiple sets of OptionButtons |
|
|
302 | (1) |
|
Using a SpinButton and a TextBox |
|
|
303 | (2) |
|
Using a UserForm as a progress indicator |
|
|
305 | (3) |
|
Creating a tabbed dialog box |
|
|
308 | (2) |
|
Displaying a chart in a UserForm |
|
|
310 | (1) |
|
|
|
311 | (2) |
|
Chapter 19 Accessing Your Macros through the User Interface |
|
|
313 | (18) |
|
What Happened to CommandBars? |
|
|
313 | (1) |
|
|
|
314 | (7) |
|
Customizing the Ribbon manually |
|
|
314 | (2) |
|
Customizing the Ribbon with XML |
|
|
316 | (5) |
|
Customizing Shortcut Menus |
|
|
321 | (5) |
|
Commanding the CommandBars collection |
|
|
321 | (1) |
|
Listing all shortcut menus |
|
|
321 | (1) |
|
|
|
322 | (1) |
|
Referring to controls in a CommandBar |
|
|
323 | (1) |
|
Properties of CommandBar controls |
|
|
324 | (2) |
|
VBA Shortcut Menu Examples |
|
|
326 | (3) |
|
Adding a new item to the Cell shortcut menu |
|
|
326 | (2) |
|
Disabling a shortcut menu |
|
|
328 | (1) |
|
Creating a Custom Toolbar |
|
|
329 | (2) |
|
Part V Putting It All Together |
|
|
331 | (30) |
|
Chapter 20 Creating Worksheet Functions --- and Living to Tell about It |
|
|
333 | (16) |
|
Why Create Custom Functions? |
|
|
333 | (1) |
|
Understanding VBA Function Basics |
|
|
334 | (1) |
|
|
|
335 | (1) |
|
Working with Function Arguments |
|
|
335 | (1) |
|
|
|
336 | (7) |
|
A function with no argument |
|
|
336 | (1) |
|
A function with one argument |
|
|
336 | (2) |
|
A function with two arguments |
|
|
338 | (1) |
|
A function with a range argument |
|
|
339 | (1) |
|
A function with an optional argument |
|
|
340 | (2) |
|
A function with an indefinite number of arguments |
|
|
342 | (1) |
|
Functions That Return an Array |
|
|
343 | (2) |
|
Returning an array of month names |
|
|
343 | (1) |
|
|
|
344 | (1) |
|
Using the Insert Function Dialog Box |
|
|
345 | (4) |
|
Displaying the function's description |
|
|
346 | (1) |
|
|
|
347 | (2) |
|
Chapter 21 Creating Excel Add-Ins |
|
|
349 | (12) |
|
Okay...So What's an Add-In? |
|
|
349 | (1) |
|
|
|
350 | (1) |
|
|
|
351 | (1) |
|
|
|
352 | (1) |
|
|
|
353 | (8) |
|
|
|
353 | (2) |
|
|
|
355 | (1) |
|
Adding descriptive information |
|
|
356 | (1) |
|
|
|
357 | (1) |
|
|
|
357 | (1) |
|
|
|
357 | (1) |
|
|
|
358 | (1) |
|
|
|
359 | (2) |
|
|
|
361 | (10) |
|
Chapter 22 Ten VBA Questions (And Answers) |
|
|
363 | (8) |
|
Chapter 23 (Almost) Ten Excel Resources |
|
|
367 | (1) |
|
|
|
367 | (1) |
|
Microsoft Product Support |
|
|
367 | (1) |
|
|
|
368 | (1) |
|
|
|
369 | (1) |
|
|
|
369 | (1) |
|
|
|
369 | (1) |
|
|
|
369 | (1) |
|
|
|
370 | (1) |
|
|
|
370 | (1) |
|
Bonus Chapters See companion Web site (www.dummies.com/go/excelubaprogramming/d2e) |
|
|
|
Bonus Chapter 1 Working with Colors Companion web site |
|
|
|
Bonus Chapter 2 Ten VBA Do's and Don'ts companion Web site |
|
|
| Index |
|
371 | |