Cover image for Excel 2007 VBA programming for dummies
Title:
Excel 2007 VBA programming for dummies
Personal Author:
Series:
--For dummies
Publication Information:
New York, NY : Wiley, 2007
Physical Description:
xvi, 368 p. : ill. ; 23 cm.
ISBN:
9780470046746

Available:*

Library
Item Barcode
Call Number
Material Type
Item Category 1
Status
Searching...
30000010179056 HF5548.4.M523 W348 2007 Open Access Book Book
Searching...

On Order

Summary

Summary

Step-by-step instructions for creating VBA macros

Harness the power of VBA and create custom Excel applications

Make Excel 2007 work for you! This clear, nonintimidating guide shows you how to use VBA to create Excel apps that look and work the way you want. Packed with plenty of sample programs, it explains how to work with range objects, control program flow, develop custom dialog boxes, create custom toolbars and menus, and much more.

Discover how to

Grasp essential programming concepts Use the Visual Basic Editor Navigate the new Excel user interface Communicate with your users Deal with errors and bugs


Author Notes

John Walkenbach is a leading authority on spreadsheet software and creator of the award-winning Power Utility Pak.


Table of Contents

Introductionp. 1
Is This the Right Book?p. 1
So You Want to Be a Programmerp. 2
Why Bother?p. 2
What I Assume about Youp. 3
Obligatory Typographical Conventions Sectionp. 4
Check Your Security Settingsp. 4
How This Book Is Organizedp. 6
Part I Introducing VBAp. 6
Part II How VBA Works with Excelp. 6
Part III Programming Conceptsp. 6
Part IV Communicating with Your Usersp. 6
Part V Putting It All Togetherp. 7
Part VI The Part of Tensp. 7
Marginal Iconsp. 7
Get the Sample Filesp. 8
Now What?p. 8
Part I Introducing VBAp. 9
Chapter 1 What Is VBA?p. 11
Okay, So What Is VBA?p. 11
What Can You Do with VBA?p. 12
Inserting a bunch of textp. 13
Automating a task you perform frequentlyp. 13
Automating repetitive operationsp. 13
Creating a custom commandp. 13
Creating a custom buttonp. 14
Developing new worksheet functionsp. 14
Creating complete, macro-driven applicationsp. 14
Creating custom add-ins for Excelp. 14
Advantages and Disadvantages of VBAp. 14
VBA advantagesp. 15
VBA disadvantagesp. 15
VBA in a Nutshellp. 16
An Excursion into Versionsp. 18
Chapter 2 Jumping Right Inp. 21
First Things Firstp. 21
What You'll Be Doingp. 22
Taking the First Stepsp. 23
Recording the Macrop. 23
Testing the Macrop. 25
Examining the Macrop. 25
Modifying the Macrop. 28
Saving Workbooks that Contain Macrosp. 29
Understanding Macro Securityp. 29
More about the NameAndTime Macrop. 31
Part II How VBA Works with Excelp. 33
Chapter 3 Working In the Visual Basic Editorp. 35
What Is the Visual Basic Editor?p. 35
Activating the VBEp. 35
Understanding VBE componentsp. 36
Working with the Project Explorerp. 38
Adding a new VBA modulep. 39
Removing a VBA modulep. 39
Exporting and importing objectsp. 40
Working with a Code Windowp. 40
Minimizing and maximizing windowsp. 40
Creating a modulep. 42
Getting VBA code into a modulep. 42
Entering code directlyp. 43
Using the macro recorderp. 45
Copying VBA codep. 47
Customizing the VBA Environmentp. 47
Using the Editor tabp. 48
Using the Editor Format tabp. 50
Using the General tabp. 52
Using the Docking tabp. 52
Chapter 4 Introducing the Excel Object Modelp. 53
Excel Is an Object?p. 54
Climbing the Object Hierarchyp. 54
Wrapping Your Mind around Collectionsp. 56
Referring to Objectsp. 56
Navigating through the hierarchyp. 57
Simplifying object referencesp. 58
Diving into Object Properties and Methodsp. 59
Object propertiesp. 59
Object methodsp. 62
Object eventsp. 63
Finding Out Morep. 63
Using VBA's Help systemp. 63
Using the Object Browserp. 64
Chapter 5 VBA Sub and Function Proceduresp. 67
Subs versus Functionsp. 67
Looking at Sub proceduresp. 68
Looking at Function proceduresp. 68
Naming Subs and Functionsp. 69
Executing Sub proceduresp. 69
Executing the Sub procedure directlyp. 71
Executing the procedure from the Macro dialog boxp. 72
Executing a macro by using a shortcut keyp. 72
Executing the procedure from a button or shapep. 74
Executing the procedure from another procedurep. 76
Executing Function proceduresp. 76
Calling the function from a Sub procedurep. 77
Calling a function from a worksheet formulap. 77
Chapter 6 Using the Excel Macro Recorderp. 79
Is It Live or Is It VBA?p. 79
Recording Basicsp. 80
Preparing to Recordp. 82
Relative or Absolute?p. 82
Recording in absolute modep. 82
Recording in relative modep. 83
What Gets Recorded?p. 85
Recording Optionsp. 86
Macro namep. 87
Shortcut keyp. 87
Store Macro Inp. 87
Descriptionp. 87
Is This Thing Efficient?p. 88
Part III Programming Conceptsp. 91
Chapter 7 Essential VBA Language Elementsp. 93
Using Comments in Your VBA Codep. 93
Using Variables, Constants, and Data Typesp. 95
Understanding variablesp. 95
What are VBA's data types?p. 97
Declaring and scoping variablesp. 98
Working with constantsp. 103
Working with stringsp. 105
Working with datesp. 106
Using Assignment Statementsp. 106
Assignment statement examplesp. 107
About that equal signp. 107
Other operatorsp. 108
Working with Arraysp. 109
Declaring arraysp. 109
Multidimensional arraysp. 110
Dynamic arraysp. 111
Using Labelsp. 111
Chapter 8 Working with Range Objectsp. 113
A Quick Reviewp. 113
Other Ways to Refer to a Rangep. 114
The Cells propertyp. 115
The Offset propertyp. 116
Referring to entire columns and rowsp. 116
Some Useful Range Object Propertiesp. 117
The Value propertyp. 117
The Text propertyp. 118
The Count propertyp. 118
The Column and Row propertiesp. 118
The Address propertyp. 119
The HasFormula propertyp. 119
The Font propertyp. 120
The Interior propertyp. 120
The Formula propertyp. 121
The NumberFormat propertyp. 121
Some Useful Range Object Methodsp. 122
The Select methodp. 122
The Copy and Paste methodsp. 123
The Clear methodp. 123
The Delete methodp. 124
Chapter 9 Using VBA and Worksheet Functionsp. 125
What Is a Function?p. 125
Using Built-in VBA Functionsp. 126
VBA function examplesp. 126
VBA functions that do more than return a valuep. 128
Discovering VBA functionsp. 129
Using Worksheet Functions in VBAp. 132
Worksheet function examplesp. 133
Entering worksheet functionsp. 136
More about Using Worksheet Functionsp. 136
Using Custom Functionsp. 137
Chapter 10 Controlling Program Flow and Making Decisionsp. 139
Going with the Flow, Dudep. 139
The GoTo Statementp. 140
Decisions, decisionsp. 141
The If-Then structurep. 141
The Select Case structurep. 146
Knocking Your Code for a Loopp. 149
For-Next loopsp. 150
Do-While loopp. 153
Do-Until loopp. 154
Looping through a Collectionp. 155
Chapter 11 Automatic Procedures and Eventsp. 157
Preparing for the Big Eventp. 157
Are events useful?p. 159
Programming event-handler proceduresp. 160
Where Does the VBA Code Go?p. 160
Writing an Event-Handler Procedurep. 161
Introductory Examplesp. 163
The Open event for a workbookp. 163
The BeforeClose event for a workbookp. 165
The BeforeSave event for a workbookp. 165
Examples of Activation Eventsp. 166
Activate and deactivate events in a sheetp. 166
Activate and deactivate events in a workbookp. 167
Workbook activation eventsp. 168
Other Worksheet-Related Eventsp. 169
The BeforeDoubleClick eventp. 169
The BeforeRightClick eventp. 169
The Change eventp. 170
Events Not Associated with Objectsp. 172
The OnTime eventp. 172
Keypress eventsp. 174
Chapter 12 Error-Handling Techniquesp. 177
Types of Errorsp. 177
An Erroneous Examplep. 178
The macro's not quite perfectp. 179
The macro is still not perfectp. 180
Is the macro perfect yet?p. 180
Giving up on perfectionp. 181
Handling Errors Another Wayp. 182
Revisiting the EnterSquareRoot procedurep. 182
About the On Error statementp. 183
Handling Errors: The Detailsp. 184
Resuming after an errorp. 184
Error handling in a nutshellp. 186
Knowing when to ignore errorsp. 186
Identifying specific errorsp. 187
An Intentional Errorp. 188
Chapter 13 Bug Extermination Techniquesp. 191
Species of Bugsp. 191
Identifying Bugsp. 192
Debugging Techniquesp. 193
Examining your codep. 193
Using the MsgBox functionp. 194
Inserting Debug.Print statementsp. 195
Using the VBA debuggerp. 196
About the Debuggerp. 196
Setting breakpoints in your codep. 196
Using the Watch windowp. 199
Using the Locals Windowp. 201
Bug Reduction Tipsp. 201
Chapter 14 VBA Programming Examplesp. 203
Working with Rangesp. 203
Copying a rangep. 204
Copying a variable-sized rangep. 205
Selecting to the end of a row or columnp. 206
Selecting a row or columnp. 207
Moving a rangep. 207
Looping through a range efficientlyp. 208
Prompting for a cell valuep. 209
Determining the selection typep. 210
Identifying a multiple selectionp. 211
Changing Excel Settingsp. 211
Changing Boolean settingsp. 212
Changing non-Boolean settingsp. 212
Working with Chartsp. 213
Modifying the chart typep. 214
Looping through the ChartObjects collectionp. 214
Modifying chart propertiesp. 215
Applying chart formattingp. 215
VBA Speed Tipsp. 216
Turning off screen updatingp. 216
Turning off automatic calculationp. 217
Eliminating those pesky alert messagesp. 218
Simplifying object referencesp. 219
Declaring variable typesp. 219
Using the With-End With structurep. 220
Part IV Communicating with Your Usersp. 221
Chapter 15 Simple Dialog Boxesp. 223
Why Create UserForms?p. 223
The MsgBox Functionp. 224
Displaying a simple message boxp. 225
Getting a response from a message boxp. 225
Customizing message boxesp. 226
The InputBox Functionp. 229
InputBox syntaxp. 229
An InputBox examplep. 229
The GetOpenFilename Methodp. 231
The syntaxp. 232
A GetOpenFilename examplep. 232
Selecting multiple filesp. 234
The GetSaveAsFilename Methodp. 235
Getting a Folder Namep. 236
Displaying Excel's Built-in Dialog Boxesp. 236
Chapter 16 UserForm Basicsp. 239
Knowing When to Use a UserFormp. 239
Creating UserForms: An Overviewp. 240
Working with UserFormsp. 241
Inserting a new UserFormp. 241
Adding controls to a UserFormp. 242
Changing properties for a UserForm controlp. 243
Viewing the UserForm Code windowp. 244
Displaying a UserFormp. 245
Using information from a UserFormp. 245
A UserForm Examplep. 246
Creating the UserFormp. 246
Adding the CommandButtonsp. 247
Adding the OptionButtonsp. 248
Adding event-handler proceduresp. 250
Creating a macro to display the dialog boxp. 251
Making the macro availablep. 252
Testing the macrop. 253
Chapter 17 Using UserForm Controlsp. 255
Getting Started with Dialog Box Controlsp. 255
Adding controlsp. 255
Introducing control propertiesp. 257
Dialog Box Controls: The Detailsp. 259
CheckBox controlp. 259
ComboBox controlp. 260
CommandButton controlp. 261
Frame controlp. 262
Image controlp. 262
Label controlp. 263
ListBox controlp. 264
MultiPage controlp. 265
OptionButton controlp. 266
RefEdit controlp. 267
ScrollBar controlp. 267
SpinButton controlp. 268
TabStrip controlp. 269
TextBox controlp. 269
ToggleButton controlp. 270
Working with Dialog Box Controlsp. 270
Moving and resizing controlsp. 270
Aligning and spacing controlsp. 271
Accommodating keyboard usersp. 272
Testing a UserFormp. 273
Dialog Box Aestheticsp. 274
Chapter 18 UserForm Techniques and Tricksp. 275
Using Dialog Boxesp. 275
A UserForm Examplep. 275
Creating the dialog boxp. 276
Writing code to display the dialog boxp. 278
Making the macro availablep. 279
Trying out your dialog boxp. 279
Adding event-handler proceduresp. 280
Validating the datap. 282
Now the dialog box worksp. 282
More UserForm Examplesp. 282
A ListBox examplep. 282
Selecting a rangep. 287
Using multiple sets of OptionButtonsp. 288
Using a SpinButton and a TextBoxp. 289
Using a UserForm as a progress indicatorp. 291
Creating a tabbed dialog boxp. 295
Displaying a chart in a dialog boxp. 296
A Dialog Box Checklistp. 297
Chapter 19 Accessing Your Macros Through the User Interfacep. 299
CommandBars and Excel 2007p. 299
Excel 2007 Ribbon Customizationp. 301
Working with CommandBarsp. 304
Commanding the CommandBars collectionp. 304
Listing all shortcut menusp. 304
Referring to CommandBarsp. 305
Referring to controls in a CommandBarp. 306
Properties of CommandBar controlsp. 307
VBA Shortcut Menu Examplesp. 309
Resetting all built-in right-click menusp. 309
Adding a new item to the Cell shortcut menup. 309
Disabling a shortcut menup. 311
Creating a Custom Toolbarp. 312
Part V Putting It Alt Togetherp. 315
Chapter 20 Creating Worksheet Functions and Living to Tell about Itp. 317
Why Create Custom Functions?p. 317
Understanding VBA Function Basicsp. 318
Writing Functionsp. 319
Working with Function Argumentsp. 319
Function Examplesp. 320
A function with no argumentp. 320
A function with one argumentp. 320
A function with two argumentsp. 322
A function with a range argumentp. 323
A function with an optional argumentp. 324
A function with an indefinite number of argumentsp. 326
Functions That Return an Arrayp. 327
Returning an array of month namesp. 327
Returning a sorted listp. 328
Using the Insert Function Dialog Boxp. 330
Displaying the function's descriptionp. 330
Argument descriptionsp. 331
Chapter 21 Creating Excel Add-Insp. 333
Okay...So What's an Add-In?p. 333
Why Create Add-Ins?p. 334
Working with Add-Insp. 335
Add-in Basicsp. 336
An Add-in Examplep. 337
Setting up the workbookp. 337
Testing the workbookp. 339
Adding descriptive informationp. 340
Protecting the VBA codep. 341
Creating the add-inp. 341
Opening the add-inp. 341
Distributing the add-inp. 342
Modifying the add-inp. 342
Part VI The Part of Tensp. 345
Chapter 22 Ten VBA Questions (And Answers)p. 347
The Top Ten Questions about VBAp. 347
Chapter 23 (Almost) Ten Excel Resourcesp. 351
The VBA Help Systemp. 351
Microsoft Product Supportp. 351
Internet Newsgroupsp. 352
Internet Web Sitesp. 353
Excel Blogsp. 353
Googlep. 353
Local User Groupsp. 354
My Other Bookp. 354
Indexp. 355