- •About the Author
- •About the Technical Editor
- •Credits
- •Is This Book for You?
- •Software Versions
- •Conventions This Book Uses
- •What the Icons Mean
- •How This Book Is Organized
- •How to Use This Book
- •What’s on the Companion CD
- •What Is Excel Good For?
- •What’s New in Excel 2010?
- •Moving around a Worksheet
- •Introducing the Ribbon
- •Using Shortcut Menus
- •Customizing Your Quick Access Toolbar
- •Working with Dialog Boxes
- •Using the Task Pane
- •Creating Your First Excel Worksheet
- •Entering Text and Values into Your Worksheets
- •Entering Dates and Times into Your Worksheets
- •Modifying Cell Contents
- •Applying Number Formatting
- •Controlling the Worksheet View
- •Working with Rows and Columns
- •Understanding Cells and Ranges
- •Copying or Moving Ranges
- •Using Names to Work with Ranges
- •Adding Comments to Cells
- •What Is a Table?
- •Creating a Table
- •Changing the Look of a Table
- •Working with Tables
- •Getting to Know the Formatting Tools
- •Changing Text Alignment
- •Using Colors and Shading
- •Adding Borders and Lines
- •Adding a Background Image to a Worksheet
- •Using Named Styles for Easier Formatting
- •Understanding Document Themes
- •Creating a New Workbook
- •Opening an Existing Workbook
- •Saving a Workbook
- •Using AutoRecover
- •Specifying a Password
- •Organizing Your Files
- •Other Workbook Info Options
- •Closing Workbooks
- •Safeguarding Your Work
- •Excel File Compatibility
- •Exploring Excel Templates
- •Understanding Custom Excel Templates
- •Printing with One Click
- •Changing Your Page View
- •Adjusting Common Page Setup Settings
- •Adding a Header or Footer to Your Reports
- •Copying Page Setup Settings across Sheets
- •Preventing Certain Cells from Being Printed
- •Preventing Objects from Being Printed
- •Creating Custom Views of Your Worksheet
- •Understanding Formula Basics
- •Entering Formulas into Your Worksheets
- •Editing Formulas
- •Using Cell References in Formulas
- •Using Formulas in Tables
- •Correcting Common Formula Errors
- •Using Advanced Naming Techniques
- •Tips for Working with Formulas
- •A Few Words about Text
- •Text Functions
- •Advanced Text Formulas
- •Date-Related Worksheet Functions
- •Time-Related Functions
- •Basic Counting Formulas
- •Advanced Counting Formulas
- •Summing Formulas
- •Conditional Sums Using a Single Criterion
- •Conditional Sums Using Multiple Criteria
- •Introducing Lookup Formulas
- •Functions Relevant to Lookups
- •Basic Lookup Formulas
- •Specialized Lookup Formulas
- •The Time Value of Money
- •Loan Calculations
- •Investment Calculations
- •Depreciation Calculations
- •Understanding Array Formulas
- •Understanding the Dimensions of an Array
- •Naming Array Constants
- •Working with Array Formulas
- •Using Multicell Array Formulas
- •Using Single-Cell Array Formulas
- •Working with Multicell Array Formulas
- •What Is a Chart?
- •Understanding How Excel Handles Charts
- •Creating a Chart
- •Working with Charts
- •Understanding Chart Types
- •Learning More
- •Selecting Chart Elements
- •User Interface Choices for Modifying Chart Elements
- •Modifying the Chart Area
- •Modifying the Plot Area
- •Working with Chart Titles
- •Working with a Legend
- •Working with Gridlines
- •Modifying the Axes
- •Working with Data Series
- •Creating Chart Templates
- •Learning Some Chart-Making Tricks
- •About Conditional Formatting
- •Specifying Conditional Formatting
- •Conditional Formats That Use Graphics
- •Creating Formula-Based Rules
- •Working with Conditional Formats
- •Sparkline Types
- •Creating Sparklines
- •Customizing Sparklines
- •Specifying a Date Axis
- •Auto-Updating Sparklines
- •Displaying a Sparkline for a Dynamic Range
- •Using Shapes
- •Using SmartArt
- •Using WordArt
- •Working with Other Graphic Types
- •Using the Equation Editor
- •Customizing the Ribbon
- •About Number Formatting
- •Creating a Custom Number Format
- •Custom Number Format Examples
- •About Data Validation
- •Specifying Validation Criteria
- •Types of Validation Criteria You Can Apply
- •Creating a Drop-Down List
- •Using Formulas for Data Validation Rules
- •Understanding Cell References
- •Data Validation Formula Examples
- •Introducing Worksheet Outlines
- •Creating an Outline
- •Working with Outlines
- •Linking Workbooks
- •Creating External Reference Formulas
- •Working with External Reference Formulas
- •Consolidating Worksheets
- •Understanding the Different Web Formats
- •Opening an HTML File
- •Working with Hyperlinks
- •Using Web Queries
- •Other Internet-Related Features
- •Copying and Pasting
- •Copying from Excel to Word
- •Embedding Objects in a Worksheet
- •Using Excel on a Network
- •Understanding File Reservations
- •Sharing Workbooks
- •Tracking Workbook Changes
- •Types of Protection
- •Protecting a Worksheet
- •Protecting a Workbook
- •VB Project Protection
- •Related Topics
- •Using Excel Auditing Tools
- •Searching and Replacing
- •Spell Checking Your Worksheets
- •Using AutoCorrect
- •Understanding External Database Files
- •Importing Access Tables
- •Retrieving Data with Query: An Example
- •Working with Data Returned by Query
- •Using Query without the Wizard
- •Learning More about Query
- •About Pivot Tables
- •Creating a Pivot Table
- •More Pivot Table Examples
- •Learning More
- •Working with Non-Numeric Data
- •Grouping Pivot Table Items
- •Creating a Frequency Distribution
- •Filtering Pivot Tables with Slicers
- •Referencing Cells within a Pivot Table
- •Creating Pivot Charts
- •Another Pivot Table Example
- •Producing a Report with a Pivot Table
- •A What-If Example
- •Types of What-If Analyses
- •Manual What-If Analysis
- •Creating Data Tables
- •Using Scenario Manager
- •What-If Analysis, in Reverse
- •Single-Cell Goal Seeking
- •Introducing Solver
- •Solver Examples
- •Installing the Analysis ToolPak Add-in
- •Using the Analysis Tools
- •Introducing the Analysis ToolPak Tools
- •Introducing VBA Macros
- •Displaying the Developer Tab
- •About Macro Security
- •Saving Workbooks That Contain Macros
- •Two Types of VBA Macros
- •Creating VBA Macros
- •Learning More
- •Overview of VBA Functions
- •An Introductory Example
- •About Function Procedures
- •Executing Function Procedures
- •Function Procedure Arguments
- •Debugging Custom Functions
- •Inserting Custom Functions
- •Learning More
- •Why Create UserForms?
- •UserForm Alternatives
- •Creating UserForms: An Overview
- •A UserForm Example
- •Another UserForm Example
- •More on Creating UserForms
- •Learning More
- •Why Use Controls on a Worksheet?
- •Using Controls
- •Reviewing the Available ActiveX Controls
- •Understanding Events
- •Entering Event-Handler VBA Code
- •Using Workbook-Level Events
- •Working with Worksheet Events
- •Using Non-Object Events
- •Working with Ranges
- •Working with Workbooks
- •Working with Charts
- •VBA Speed Tips
- •What Is an Add-In?
- •Working with Add-Ins
- •Why Create Add-Ins?
- •Creating Add-Ins
- •An Add-In Example
- •System Requirements
- •Using the CD
- •What’s on the CD
- •Troubleshooting
- •The Excel Help System
- •Microsoft Technical Support
- •Internet Newsgroups
- •Internet Web sites
- •End-User License Agreement
Part VI: Programming Excel with VBA
4.Double-click the button.
The VB Editor window is activated, and an empty Sub procedure is created.
5.Enter the following VBA statement before the End Sub statement:
MsgBox “Hello. You clicked the command button.”
6.Press Alt+F11 to return to Excel.
7.(Optional) Adjust any other properties for the CommandButton, using the Properties window. Choose Developer Controls Properties if the Properties window isn’t visible.
8.Click the Design Mode button in the Developer Controls section to exit design mode.
After performing the preceding steps, click the CommandButton to display the message box.
Note
You must enter the VBA code manually. You can’t create macros for controls using the VBA macro recorder. However, you can record a macro and then execute it from an event procedure. For example, if you record a macro named FormatCells, insert Call FormatCells as a VBA statement. Or, you can copy the recorded code and paste it to your event procedure. n
Reviewing the Available ActiveX Controls
The sections that follow describe the ActiveX controls that are available for use in your worksheets.
On the CD
The companion CD-ROM contains a file that includes examples of all the ActiveX controls. This file is named worksheet controls.xlsm.
CheckBox
A CheckBox control is useful for getting a binary choice: YES or NO, TRUE or FALSE, ON or OFF, and so on.
The following is a description of the most useful properties of a CheckBox control:
•Accelerator: A letter that enables the user to change the value of the control by using the keyboard. For example, if the accelerator is A, pressing Alt+A changes the value of the CheckBox control. The accelerator letter is underlined in the Caption of the control.
866
Chapter 42: Using UserForm Controls in a Worksheet
•LinkedCell: The worksheet cell that’s linked to the CheckBox. The cell displays TRUE if the control is checked or FALSE if the control is not checked.
ComboBox
A ComboBox control is similar to a ListBox control. A ComboBox, however, is a drop-down box, and it displays only one item at a time. Another difference is that the user may be allowed to enter a value that does not appear in the list of items.
Figure 42.5 shows a ComboBox control that uses the range D1:D12 for the ListFillRange and cell A1 for the LinkedCell.
FIGURE 42.5
A ComboBox control.
The following is a description of the most useful properties of a ComboBox control:
•BoundColumn: If the ListFillRange contains multiple columns, this property determines which column contains the returned value.
•ColumnCount: The number of columns in the list.
•LinkedCell: The worksheet cell that displays the selected item.
•ListFillRange: The worksheet range that contains the list items.
•ListRows: The number of items to display when the list drops down.
•ListStyle: Determines the appearance of the list items.
•Style: Determines whether the control acts like a drop-down list or a ComboBox. A drop-down list doesn’t allow the user to enter a new value.
867
Part VI: Programming Excel with VBA
Cross-Reference
You can also create a drop-down list directly in a cell, by using data validation. See Chapter 25 for details. n
CommandButton
A CommandButton control is used to execute a macro. When a CommandButton is clicked, it executes a macro with a name that is made up of the CommandButton name, an underscore, and the word Click. For example, if a CommandButton is named MyButton, clicking it executes the macro named MyButton_Click. This macro is stored in the code module for the sheet that contains the CommandButton.
Image
An Image control is used to display an image.
These are the most useful properties of an Image control:
•AutoSize: If TRUE, the Image control is resized automatically to fit the image.
•Picture: The path to the image file. Click the button in the Properties window, and Excel displays a dialog box so you can locate the image. Or, copy the image to the Clipboard, select the Picture property in the Properties window, and press Ctrl+V.
Tip
You can also insert an image on a worksheet by choosing Insert Illustrations Picture. In fact, using an Image control offers no real advantage. n
Label
A Label control simply displays text. This control isn’t a very useful for use on worksheets, and a TextBox control (described later in this list) gives you more versatility.
ListBox
A ListBox control presents a list of items, and the user can select an item (or multiple items). It’s similar to a ComboBox. The main difference is that a ListBox displays more than one item at a time.
The following is a description of the most useful properties of a ListBox control:
•BoundColumn: If the list contains multiple columns, this property determines which column contains the returned value.
•ColumnCount: The number of columns in the list.
868
Chapter 42: Using UserForm Controls in a Worksheet
•IntegralHeight: This is TRUE if the height of the ListBox adjusts automatically to display full lines of text when the list is scrolled vertically. If FALSE, the ListBox may display partial lines of text when it is scrolled vertically.
•LinkedCell: The worksheet cell that displays the selected item.
•ListFillRange: The worksheet range that contains the list items.
•ListStyle: Determines the appearance of the list items.
•MultiSelect: Determines whether the user can select multiple items from the list.
Note
If you use a MultiSelect ListBox, you can’t specify a LinkedCell; you need to write a macro to determine which items are selected. n
OptionButton
OptionButton controls are useful when the user needs to select from a small number of items. OptionButtons are always used in groups of at least two.
The following is a description of the most useful properties of an OptionButton control:
•Accelerator: A letter that lets the user select the option by using the keyboard. For example, if the accelerator for an OptionButton is C, pressing Alt+C selects the control.
•GroupName: A name that identifies an OptionButton as being associated with other OptionButtons with the same GroupName property.
•LinkedCell: The worksheet cell that’s linked to the OptionButton. The cell displays TRUE if the control is selected or FALSE if the control isn’t selected.
Note
If your worksheet contains more than one set of OptionButton controls, you must ensure that each set of OptionButtons has a different GroupName property. Otherwise, all OptionButtons become part of the same set. n
ScrollBar
A ScrollBar control is useful for specifying a cell value. Figure 42.6 shows a worksheet with three ScrollBar controls. These ScrollBars are used to change the color in the rectangle shape. The value of the ScrollBars determines the red, green, or blue component of the rectangle’s color. This example uses a few simple macros to change the colors.
869
Part VI: Programming Excel with VBA
FIGURE 42.6
This worksheet has three ScrollBar controls.
The following is a description of the most useful properties of a ScrollBar control:
•Value: The current value of the control
•Min: The minimum value for the control
•Max: The maximum value for the control
•LinkedCell: The worksheet cell that displays the value of the control
•SmallChange: The amount that the control’s value is changed by a click
•LargeChange: The amount that the control’s value is changed by clicking either side of the button
The ScrollBar control is most useful for selecting a value that extends across a wide range of possible values.
SpinButton
A SpinButton control lets the user select a value by clicking the control, which has two arrows (one to increase the value and the other to decrease the value). A SpinButton can display either horizontally or vertically.
The following is a description of the most useful properties of a SpinButton control:
•Value: The current value of the control.
•Min: The minimum value of the control.
870
Chapter 42: Using UserForm Controls in a Worksheet
•Max: The maximum value of the control.
•LinkedCell: The worksheet cell that displays the value of the control.
•SmallChange: The amount that the control’s value is changed by a click. Usually, this property is set to 1, but you can make it any value.
TextBox
On the surface, a TextBox control may not seem useful. After all, it simply contains text — you can usually use worksheet cells to get text input. In fact, TextBox controls are useful not so much for input control but rather for output control. Because a TextBox can have scroll bars, you can use a TextBox to display a great deal of information in a small area.
Figure 42.7 shows a TextBox control that contains Lincoln’s Gettysburg Address. Notice the vertical scroll bar, displayed using the ScrollBars property.
FIGURE 42.7
A TextBox control with a vertical scroll bar.
The following is a description of the most useful properties of a TextBox control:
•AutoSize: Determines whether the control adjusts its size automatically, based on the amount of text.
•IntegralHeight: If TRUE, the height of the TextBox adjusts automatically to display full lines of text when the list is scrolled vertically. If FALSE, the ListBox may display partial lines of text when it is scrolled vertically.
•MaxLength: The maximum number of characters allowed in the TextBox. If 0, no limit exists on the number of characters.
871
Part VI: Programming Excel with VBA
•MultiLine: If True, the TextBox can display more than one line of text.
•TextAlign: Determines how the text is aligned in the TextBox.
•WordWrap: Determines whether the control allows word wrap.
•ScrollBars: Determines the type of ScrollBars for the control: horizontal, vertical, both, or none.
ToggleButton
A ToggleButton control has two states: on or off. Clicking the button toggles between these two states, and the button changes its appearance. Its value is either TRUE (pressed) or FALSE (not pressed). You can often use a ToggleButton in place of a CheckBox control.
872