Excel VBA Programming For Dummies
Реклама. ООО «ЛитРес», ИНН: 7719571260.
Оглавление
Dick Kusleika. Excel VBA Programming For Dummies
Excel® VBA Programming For Dummies® To view this book's Cheat Sheet, simply go to www.dummies.com and search for “Excel VBA Programming For Dummies Cheat Sheet” in the Search box. Table of Contents
List of Tables
List of Illustrations
Guide
Pages
Introduction
About This Book
Typographical conventions
Macro security
Foolish Assumptions
Icons Used in This Book
Beyond the Book
Where to Go from Here
Starting Excel VBA Programming
Getting to Know VBA
Understanding VBA Basics
A FEW WORDS ABOUT TERMINOLOGY
Knowing What VBA Can Do
Inserting a bunch of text
Automating a task you perform frequently
Automating repetitive operations
Creating a custom command
Creating a custom button
Developing new worksheet functions
Creating custom add-ins for Excel
Getting the Most from VBA
Knowing what VBA does best
Recognizing the disadvantages of using VBA
Understanding VBA Concepts
Ensuring Excel Compatibility
Building Simple Macros
Displaying the Developer Tab
Creating a Macro
Preparing the Environment
Recording a Macro
Running the Macro
Viewing a Macro in the Visual Basic Editor
HEY, I DIDN’T RECORD THAT!
Modifying the Macro
Saving Workbooks That Contain Macros
Understanding Macro Security
Employing VBA with Excel
Working in the Visual Basic Editor
Getting to Know the Visual Basic Editor
Activating the VBE
Exploring VBE components
Menu bar
Toolbar
Project Explorer
Code pane
Immediate window
GETTING HELP IN THE VBE
Working with the Project Explorer
Adding a new VBA module
Working with a Code Pane
Minimizing and maximizing windows
Looking at the parts of a module
Getting VBA code into a module
Entering code directly
PAUSE FOR A TERMINOLOGY BREAK
COMPILE ERROR?
Using the macro recorder
Copying VBA code
Customizing the VBE
Using the Editor tab
Using the Editor Format tab
Using the General tab
Using the Docking tab
Introducing the Excel Object Model
Working with the Excel Object Model
Understanding the object hierarchy
Referring to objects
Using Collection objects
Considering generic collection objects
Simplifying object references
Diving into Object Properties and Methods
A FAST-FOOD TAKE ON OBJECTS, PROPERTIES, AND METHODS
Setting object properties
Taking action with object methods
Triggering actions with object events
Finding Out More from VBA Resources
Using VBA’s Help system
Using the Object Browser
Automatically listing properties and methods
VBA Sub and Function Procedures
Understanding Subs versus Functions
Looking at Sub procedures
Looking at Function procedures
Naming Subs and Functions
Executing Sub procedures
Executing the Sub procedure directly
Executing the procedure from the Macro dialog box
Executing a macro by using a shortcut key
Executing the procedure from a button or shape
Executing the procedure from another procedure
Executing the procedure from the Immediate window
Executing Function Procedures
Calling the function from a Sub procedure
Calling a function from the Immediate window
Calling a function from a worksheet formula
Using the Excel Macro Recorder
Recording Basics
Preparing to Record
Choosing Between Relative and Absolute Mode
Recording in absolute mode
Recording in relative mode
Watching the Macro Recorder in Action
Specifying Recording Options for Your Macro
Streamlining Code Generated by the Macro Recorder
Programming Concepts
Essential VBA Language Elements
Using Comments in Your VBA Code
Using Variables, Constants, and Data Types
Understanding variables
What are VBA’s data types?
Declaring and scoping variables
Procedure-only variables
Module-only variables
Public variables
Static variables
Life of variables
Working with constants
Using premade constants
Working with strings
Working with dates
Using Assignment Statements
Assignment statement examples
About that equal sign
Smooth operators
Working with Arrays
Declaring arrays
Multidimensional arrays
Dynamic arrays
Using Labels
Working with Range Objects
Referring to Range Objects
Referring to a Range Using Properties
The Cells property
The Offset property
The Resize property
Working with Range Object Properties
The Value property
ASSIGNING THE VALUES IN A MULTICELL RANGE TO A VARIABLE
The Text property
The Count property
The Column and Row properties
The Address property
The HasFormula property
The Font property
The Interior property
A QUICK-AND-DIRTY COLOR PRIMER
The Formula property
The NumberFormat property
Taking Action with Range Object Methods
The Select method
The Copy and Paste methods
The Clear method
The Delete method
Using VBA and Worksheet Functions
Understanding Functions
Using Built-In VBA Functions
Working with dates and times
Displaying the system date or time
Getting a part of a date or time
Displaying the name of a month
Manipulating strings
Finding a string length
Getting a part of a string
Replacing part of a string
Identifying objects and data
Identifying the type of a selected object
Validating data
VBA functions that do more than return a value
Discovering VBA functions
Using Worksheet Functions in VBA
Worksheet function examples
Finding the maximum value in a range
Calculating a mortgage payment
Using a lookup function
Entering worksheet functions
More about using worksheet functions
Using Custom Functions
Controlling Program Flow and Making Decisions
Going with the Flow, Dude
The GoTo Statement
USING STRUCTURED PROGRAMMING TO WRITE BETTER CODE
Decisions, Decisions
The If-Then structure
If-Then examples
An If-Then-Else example
Using ElseIf
Another If-Then example
The Select Case structure
A Select Case example
A nested Select Case example
Knocking Your Code for a Loop
For-Next loops
A For-Next example
For-Next examples with a Step
A For-Next example with an Exit For statement
A nested For-Next example
Do-While loop
Do-Until loop
Using For Each-Next Loops with Collections
Automatic Procedures and Events
Preparing for the Big Event
Learning when to use event procedures
Programming event-handler procedures
Knowing Where to Put the Event Code
Writing an Event-Handler Procedure
Triggering Workbook Events
The Open event for a workbook
The BeforeClose event for a workbook
The BeforeSave event for a workbook
Using Activation Events
Activate and deactivate events in a sheet
Activate and deactivate events in a workbook
Workbook activation events
Programming Worksheet-Related Events
The BeforeDoubleClick event
The BeforeRightClick event
The Change event
Why not use data validation?
Preventing data validation from being destroyed
Understanding Events Not Associated with Objects
The OnTime event
Keypress events
Error-Handling Techniques
Types of Errors
An Erroneous Macro Example
Not-quite-perfect macros
Perfecting the macro
Giving up on perfection
Alternate Ways of Handling Errors
Revisiting the EnterSquareRoot procedure
ON ERROR NOT WORKING?
Trapping errors with the On Error statement
Handling Errors: The Details
Resuming after an error
Error handling in a nutshell
An Intentional Error
Bug Extermination Techniques
Species of Bugs
Identifying Bugs
Debugging Techniques
Examining your code
Using the MsgBox function
Inserting Debug.Print statements
Using the VBA debugger
Using the Debugger’s Tools
Setting breakpoints in your code
Using the Immediate window
Stepping through your code
Using the Watches window
Using the Locals window
Bug Reduction Tips
VBA Programming Examples
Working with Ranges
Copying a range
Copying a variable-size range
Selecting to the end of a row or column
Selecting a row or column
Moving a range
Looping through a range efficiently
Looping through a range efficiently (Part II)
Prompting for a cell value
Determining the selection type
Identifying a multiple selection
Changing Excel Settings
Changing Boolean settings
Changing non-Boolean settings
Working with Charts
AddChart versus AddChart2
Modifying the chart type
Looping through the ChartObjects collection
Modifying chart properties
Applying chart formatting
VBA Speed Tips
Turning off screen updating
Turning off automatic calculation
Eliminating those pesky alert messages
Simplifying object references
Declaring variable types
Using the With-End With structure
Communicating with Your Users
Simple Dialog Boxes
Interacting with the User in VBA
Displaying Messages with the MsgBox Function
Displaying a simple message box
Getting a response from a message box
Customizing message boxes
Getting Data with an Input Box
Understanding the InputBox syntax
Using the InputBox function
Using the InputBox method
Allowing the User to Select a File or Folder
Constructing a GetOpenFilename statement
Selecting a file with GetOpenFilename
Picking a file with GetSaveAsFilename
Getting a folder name
Displaying Excel’s Built-In Dialog Boxes
UserForm Basics
Knowing When to Use a UserForm
Creating UserForms: An Overview
Working with UserForms
Inserting a new UserForm
Adding controls to a UserForm
Changing properties for a UserForm control
Viewing the UserForm Code pane
Displaying a UserForm
Using information from a UserForm
A UserForm Example
Creating the UserForm
Adding the CommandButtons
Adding the OptionButtons
Adding event-handler procedures
Creating a macro to display the dialog box
Making the macro available
Testing the macro
Using UserForm Controls
Getting Started with Dialog Box Controls
Adding controls
Introducing control properties
Learning Dialog Box Controls Details
CheckBox control
ComboBox control
CommandButton control
Frame control
Image control
Label control
ListBox control
MultiPage control
OptionButton control
RefEdit control
ScrollBar control
SpinButton control
TabStrip control
TextBox control
ToggleButton control
Working with Dialog Box Controls
Moving and resizing controls
Aligning and spacing controls
Accommodating keyboard users
Changing the tab order
Setting hot keys
Testing a UserForm
Dialog Box Aesthetics
UserForm Techniques and Tricks
Using Dialog Boxes
A UserForm Example
Creating the dialog box
Writing code to display the dialog box
Making the macro available
Trying out your dialog box
Adding event-handler procedures
Validating the data
Now the dialog box works
A ListBox Control Example
Filling a ListBox Control
Determining the selected item
Determining multiple selections
Selecting a Range
Using Multiple Sets of Option Buttons
Using a Spin Button and a Text Box
Using a UserForm as a Progress Indicator
Creating the progress-indicator dialog box
The procedures
How this example works
Creating a Modeless Tabbed Dialog Box
Displaying a Chart in a UserForm
A Dialog Box Checklist
Accessing Your Macros through the User Interface
Customizing the Ribbon
Customizing the Ribbon manually
Adding a macro to the Ribbon
GETTING THE SOFTWARE
Customizing the Ribbon with XML
ADDING A BUTTON TO THE QUICK ACCESS TOOLBAR
Customizing the Excel UI with VBA
Adding commands to the Add-ins Ribbon tab
Adding a new item to the Cell shortcut menu
Adding customizations automatically
Understanding shortcut menus and the single document interface
Putting It All Together
Creating Worksheet Functions
Create Custom Functions to Simplify Your Work
WHAT CUSTOM WORKSHEET FUNCTIONS CAN'T DO
Understanding VBA Function Basics
Writing Functions
Working with Function Arguments
A function with no argument
A function with one argument
A function with two arguments
A function with a range argument
A function with an optional argument
DEBUGGING CUSTOM FUNCTIONS
Introducing Wrapper Functions
The NumberFormat function
The ExtractElement function
The SayIt function
The IsLike function
Working with Functions That Return an Array
Returning an array of month names
Returning a sorted list
Using the Insert Function Dialog Box
Displaying the function’s description
Adding argument descriptions
Creating Excel Add-Ins
Add-Ins Defined
Reasons to Create Add-Ins
Working with Add-Ins
Understanding Add-In Basics
Looking at an Add-In Example
Setting up the workbook
Testing the workbook
Adding descriptive information
Protecting the VBA code
Creating the add-in
Opening the add-in
Distributing the add-in
Modifying the add-in
The Part of Tens
Ten Handy Visual Basic Editor Tips
Applying Block Comments
Copying Multiple Lines of Code at Once
Jumping between Modules and Procedures
Teleporting to Your Functions
Staying in the Right Procedure
Stepping through Your Code
Stepping to a Specific Line in Your Code
Stopping Your Code at a Predefined Point
Seeing the Beginning and End of Variable Values
Turning Off Auto Syntax Check
Resources for VBA Help
Letting Excel Write Code for You
Referencing the Help System
Pilfering Code from the Internet
Leveraging User Forums
Visiting Expert Blogs
Mining YouTube for Video Training
Attending Live and Online Training Classes
Learning from the Microsoft Office Dev Center
Dissecting the Other Excel Files in Your Organization
Asking Your Local Excel Guru
Ten VBA Do’s and Don’ts
Do Declare All Variables
Don’t Confuse Passwords with Security
Do Clean Up Your Code
Don’t Put Everything in One Procedure
Do Consider Other Software
Don’t Assume That Everyone Enables Macros
Do Get in the Habit of Experimenting
Don’t Assume That Your Code Will Work with Other Excel Versions
Do Keep Your Users in Mind
Don’t Forget about Backups
Index. A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
About the Author
Dedication
Author’s Acknowledgments
WILEY END USER LICENSE AGREEMENT
Отрывок из книги
Greetings, prospective Excel programmer…
You no doubt have your reasons for picking up a book on VBA programming. Maybe you got a new job (congratulations). Maybe you’re trying to automate some of the repetitive data crunching tasks you have to do. Maybe you’re just a nerd at heart. Whatever the reason, thank you for choosing this book.
.....
An empty VBA module is like the fake food you see in the windows of some restaurants; it looks good, but it doesn’t really do much for you. Before you can do anything meaningful, you must have some VBA code in the VBA module. You can get VBA code into a VBA module in three ways:
Sometimes, the best route is the most direct one. Entering code directly involves … well, typing the code using your keyboard. Entering and editing text in a VBA module works as you might expect. You can select, copy, cut, paste, and drag-and-drop.
.....