- •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
Chapter 42: Using UserForm Controls in a Worksheet
FIGURE 42.2
Excel’s two sets of worksheet controls.
TABLE 42.1
|
ActiveX Controls |
Button |
What It Does |
|
|
CommandButton |
Inserts a CommandButton control (a clickable button) |
|
|
ComboBox |
Inserts a ComboBox control (a drop-down list) |
|
|
CheckBox |
Inserts a CheckBox control (to control Boolean options) |
|
|
ListBox |
Inserts a ListBox control (to allow the user to select an item from a list) |
|
|
TextBox |
Inserts a TextBox control (allows the user to type text) |
|
|
ScrollBar |
Inserts a ScrollBar control (to specify a value by dragging a bar) |
|
|
SpinButton |
Inserts a SpinButton control (to specify a value by clicking up or down) |
|
|
OptionButton |
Inserts an OptionButton control (to allow a user to select from multiple options) |
|
|
Label |
Inserts a Label control (a container for text) |
|
|
Image |
Inserts an Image control (to hold an image) |
ToggleButton |
Inserts a ToggleButton control (to control Boolean options) |
|
|
More Controls |
Displays a list of other ActiveX controls that are installed on your system. Not all |
|
these controls work with Excel. |
Using Controls
Adding ActiveX controls in a worksheet is easy, but you need to learn a few basic facts about how to use them.
861
Part VI: Programming Excel with VBA
Adding a control
To add a control to a worksheet, choose Developer Controls Insert. From the Insert dropdown icon list, click the control that you want to use and then drag in the worksheet to create the control. You don’t need to be too concerned about the exact size or position because you can modify those properties at any time.
Warning
Make sure that you select a control from the ActiveX controls — not from the Forms controls. If you insert a Forms control, the instructions in this chapter will not apply. n
About design mode
When you add a control to a worksheet, Excel goes into design mode. In this mode, you can adjust the properties of any controls on your worksheet, add or edit macros for the control, or change the control’s size or position.
Note
When Excel is in design mode, the Design Mode icon in the Developer Controls section appears highlighted. You can click this icon to toggle design mode on and off. n
When Excel is in design mode, the controls aren’t enabled. To test the controls, you must exit design mode by clicking the Design Mode icon. When you’re working with controls, you’ll probably need to need to switch in and out of design mode frequently.
Adjusting properties
Every control that you add has various properties that determine how it looks and behaves. You can adjust these properties only when Excel is in design mode. When you add a control to a worksheet, Excel enters design mode automatically. If you need to change a control after you exit design mode, click the Design Mode icon in the Controls section of the Developer tab.
To change the properties for a control
1.Make sure that Excel is in design mode.
2.Click the control to select it.
3.If the Properties window isn’t visible, click the Properties icon in the Controls section of the Developer tab. The Properties window appears, as shown in Figure 42.3.
4.Select the property and make the change.
862
Chapter 42: Using UserForm Controls in a Worksheet
The manner in which you change a property depends upon the property. Some properties display a drop-down list from which you can select from a list of options. Others (such as Font) provide a button that when clicked, displays a dialog box. Other properties require you to type the property value. When you change a property, the change takes effect immediately.
Tip
To find out about a particular property, select the property in the Properties window and press F1. n
The Properties window has two tabs. The Alphabetic tab displays the properties in alphabetical order. The Categorized tab displays the properties by category. Both tabs show the same properties; only the order is different.
FIGURE 42.3
Use the Properties window to adjust the properties of a control — in this case, a CommandButton control.
Common properties
Each control has its own unique set of properties. However, many controls share properties. This section describes some properties that are common to all or many controls, as set forth in Table 42.2.
Note
Some ActiveX control properties are required (for example, the Name property). In other words, you can’t leave the property empty. If a required property is missing, Excel will always tell you by displaying an error message. n
863
Part VI: Programming Excel with VBA
TABLE 42.2
|
Properties Shared by Multiple Controls |
Property |
Description |
|
|
AutoSize |
If True, the control resizes itself automatically, based on the text in its caption. |
|
|
BackColor |
The background color of the control. |
|
|
BackStyle |
The style of the background (either transparent or opaque). |
|
|
Caption |
The text that appears on the control. |
|
|
LinkedCell |
A worksheet cell that contains the current value of a control. |
|
|
ListFillRange |
A worksheet range that contains items displayed in a ListBox or ComboBox control. |
|
|
Value |
The control’s value. |
|
|
Left and Top |
Values that determine the control’s position. |
|
|
Width and Height |
Values that determine the control’s width and height. |
|
|
Visible |
If False, the control is hidden. |
|
|
Name |
The name of the control. By default, a control’s name is based on the control type. |
|
You can change the name to any valid name. However, each control’s name must |
|
be unique on the worksheet. |
|
|
Picture |
Enables you to specify a graphic image to display. |
|
|
Linking controls to cells
Often, you can use ActiveX controls in a worksheet without using any macros. Many controls have a LinkedCell property, which specifies a worksheet cell that is “linked” to the control.
For example, you may add a SpinButton control and specify cell B1 as its LinkedCell property. After doing so, cell B1 contains the value of the SpinButton, and clicking the SpinButton changes the value in cell B1. You can, of course, use the value contained in the linked cell in your formulas.
Note
When specifying the LinkedCell property in the Properties window, you can’t “point” to the linked cell in the worksheet. You must type the cell address or its name (if it has one). n
Creating macros for controls
To create a macro for a control, you must use the Visual Basic Editor (VB Editor). The macros are stored in the code module for the sheet that contains the control. For example, if you place an
864
Chapter 42: Using UserForm Controls in a Worksheet
ActiveX control on Sheet2, the VBA code for that control is stored in the Sheet2 code module. Each control can have a macro to handle any of its events. For example, a CommandButton control can have a macro for its Click event, its DblClick event, and various other events.
Tip
The easiest way to access the code module for a control is to double-click the control while in design mode. Excel displays the VB Editor and creates an empty procedure for the control’s default event. For example, the default event for a CheckBox control is the Click event. Figure 42.4 shows the autogenerated code for a control named CheckBox1, located on Sheet1. n
FIGURE 42.4
Double-clicking a control in design mode activates the VB Editor and enters an empty event-handler procedure.
The control’s name appears in the upper-left portion of the code window, and the event appears in the upper-right area. If you want to create a macro that executes when a different event occurs, select the event from the list in the upper-right area.
The following steps demonstrate how to insert a CommandButton and create a simple macro that displays a message when the button is clicked:
1.Choose Developer Controls Insert.
2.Click the CommandButton tool in the ActiveX Controls section.
3.Click and drag in the worksheet to create the button. Excel automatically enters design mode.
865