Wednesday, April 10, 2013

How to use the forms controls on a worksheet in Excel

Microsoft Excel provides several controls for dialog sheets that are useful for selecting items from a list. Examples of controls are list boxes, combo boxes, spin buttons, and scroll bars.
Enable the Developer tab
To use the form controls in Excel 2010, you have to enable the Developer tab. To do this, follow these steps:
  1. Click File, and then click Options.
    Collapse this imageExpand this image
  2. Click Customize Ribbon in the left pane.
    Collapse this imageExpand this image
  3. Select the Developer check box under Main Tabs on the right, and then click OK.
To use the forms controls in Excel 2007, you must enable the Developer tab. To do this, follow these steps:
  1. Click the Microsoft Office Button, and then click Excel Options.
    Collapse this imageExpand this image
  2. Click Popular, select the Show Developer tab in the Ribbon check box, and then click OK.
    Collapse this imageExpand this image

Set up the list, the cell link, and the index

  1. In a new worksheet, type the following items in the range H1:H20:
          H1 : Roller Skates
          H2 : VCR
          H3 : Desk
          H4 : Mug
          H5 : Car
          H6 : Washing Machine
          H7 : Rocket Launcher
          H8 : Bike
          H9 : Phone
          H10: Candle
          H11: Candy
          H12: Speakers
          H13: Dress
          H14: Blanket
          H15: Dryer
          H16: Guitar
          H17: Dryer
          H18: Tool Set
          H19: VCR
          H20: Hard Disk
    					
  2. In cell A1, type the following formula:
    =INDEX(H1:H20,G1,0)

List box example

  1. To add a list box in Excel 2010 and Excel 2007, click the Developer tab, click Insert in the Controls group, and then click List Box Form (Control) under Form Controls.
    Collapse this imageExpand this image


    To add a list box in Excel 2003 and in earlier versions of Excel, click the List Box button on the Forms toolbar. If the Forms toolbar is not visible, point to Toolbars on the View menu, and then click Forms.
  2. Click the worksheet location where you want the upper-left corner of the list box to appear, and then drag the list box to where you want the lower-right corner of the list box to be. In this example, create a list box that covers cells B2:E10.
  3. In the Controls group, click Properties.
    Collapse this imageExpand this image
  4. In the Format Object window, type the following information, and then click OK.
    1. To specify the range for the list, type H1:H20 in the Input range box.
    2. To put a number value in cell G1 (depending on which item is selected in the list), type G1 in the Cell link box.

      Note The INDEX() formula uses the value in G1 to return the correct list item.
    3. Under Selection type, make sure that the Single option is selected.

      Note The Multi and Extend options are only useful when you are using a Microsoft Visual Basic for Applications procedure to return the values of the list. Note also that the 3-D shading check box adds a three-dimensional look to the list box.
    4. Collapse this imageExpand this image
  5. The list box should display the list of items. To use the list box, click any cell so that the list box is not selected. If you click an item in the list, cell G1 is updated to a number that indicates the position of the item that is selected in the list. The INDEX formula in cell A1 uses this number to display the item's name.

Combo box example

  1. To add a combo box in Excel 2010 and Excel 2007, click the Developer tab, click Insert, and then click Combo Box under Form Controls.
    Collapse this imageExpand this image


    To add a combo box in Excel 2003 and in earlier versions of Excel, click the Combo Box button on the Forms toolbar.
  2. Click the worksheet location where you want the upper-left corner of the combo box to appear, and then drag the combo box to where you want the lower-right corner of the list box to be. In this example, create a combo box that covers cells B2:E2.
    Collapse this imageExpand this image
  3. Right-click the combo box, and then click Format Control.
    Collapse this imageExpand this image
  4. Type the following information, and then click OK:
    1. To specify the range for the list, type H1:H20 in the Input range box.
    2. To put a number value in cell G1 (depending on which item is selected in the list), type G1 in the Cell link box.

      Note The INDEX formula uses the value in G1 to return the correct list item.
    3. In the Drop down lines box, type 10. This entry determines how many items will be displayed before you have to use a scroll bar to view the other items.

      Note The 3-D shading check box is optional. It adds a three-dimensional look to the drop-down or combo box.
      Collapse this imageExpand this image
  5. The drop-down box or combo box should display the list of items. To use the drop-down box or combo box, click any cell so that the object is not selected. When you click an item in the drop-down box or combo box, cell G1 is updated to a number that indicates the position in the list of the item selected. The INDEX formula in cell A1 uses this number to display the item's name.

Spin button example

  1. To add a spin button in Excel 2010 and 2007, click the Developer tab, click Insert, and then click Spin Button under Form Controls.
    Collapse this imageExpand this image


    To add a spinner in Excel 2003 and in earlier versions of Excel, click the Spinner button on the Forms toolbar.
  2. Click the worksheet location where you want the upper-left corner of the spin button to appear, and then drag the spin button to where you want the lower-right corner of the spin button to be. In this example, create a spin button that covers cells B2: B3.
  3. Right-click the spin button, and then click Format Control.
    Collapse this imageExpand this image
  4. Type the following information, and then click OK:
    1. In the Current value box, type 1.

      This value initializes the spin button so that the INDEX formula will point to the first item in the list.
    2. In the Minimum value box, type 1.

      This value restricts the top of the spin button to the first item in the list.
    3. In the Maximum value box, type 20.

      This number specifies the maximum number of entries in the list.
    4. In the Incremental change box, type 1.

      This value controls how much the spin button control increments the current value.
    5. To put a number value in cell G1 (depending on which item is selected in the list), type G1 in the Cell link box.
      Collapse this imageExpand this image
  5. Click any cell so that the spin button is not selected. When you click the up control or down control on the spin button, cell G1 is updated to a number that indicates the current value of the spin button plus or minus the incremental change of the spin button. This number then updates the INDEX formula in cell A1 to show the next or previous item.

    The spin button value will not change if the current value is 1 and you click the down control, or if the current value is 20 and you click the up control.

Scroll bar example

  1. To add a scroll bar in Excel 2010 and Excel 2007, click the Developer tab, click Insert, and then click Scroll Bar under Form Controls.
    Collapse this imageExpand this image


    To add a scroll bar in Excel 2003 and in earlier versions of Excel, click the Scroll Bar button on the Forms toolbar.
  2. Click the worksheet location where you want the upper-left corner of the scroll bar to appear, and then drag the scroll bar to where you want the lower-right corner of the scroll bar to be. In this example, create a scroll bar that covers cells B2:B6 in height and is about one-fourth of the width of the column.
    Collapse this imageExpand this image
  3. Right-click the scroll bar, and then click Format Control.
    Collapse this imageExpand this image
  4. Type the following information, and then click OK:
    1. In the Current value box, type 1.

      This value initializes the scroll bar so that the INDEX formula will point to the first item in the list.
    2. In the Minimum value box, type 1.

      This value restricts the top of the scroll bar to the first item in the list.
    3. In the Maximum value box, type 20. This number specifies the maximum number of entries in the list.
    4. In the Incremental change box, type 1.

      This value controls how many numbers the scroll bar control increments the current value.
    5. In the Page change box, type 5. This value controls how much the current value will be incremented if you click inside the scroll bar on either side of the scroll box).
    6. To put a number value in cell G1 (depending on which item is selected in the list), type G1 in the Cell link box.
      Collapse this imageExpand this image


      Note The 3-D shading check box is optional. It adds a three-dimensional look to the scroll bar.
  5. Click any cell so that the scroll bar is not selected. When you click the up or down control on the scroll bar, cell G1 is updated to a number that indicates the current value of the scroll bar plus or minus the incremental change of the scroll bar. This number is used in the INDEX formula in cell A1 to show the item next to or before the current item. You can also drag the scroll box to change the value or click in the scroll bar on either side of the scroll box to increment it by 5 (the Page change value). The scroll bar will not change if the current value is 1 and you click the down control, or if the current value is 20 and you click the up control.

What is a form?

A form, whether printed or online, is a document designed with a standard structure and format that makes it easier to capture, organize, and edit information.
  • Printed forms contain instructions, formatting, labels, and blank spaces for writing or typing data. You can use Excel and Excel templates to create printed forms.

Types of Excel forms

There are several types of forms that you can create in Excel: data forms, worksheets that contain Form and ActiveX controls, and VBA UserForms. You can use each type of form by itself, or you can combine them in different ways to create a solution that's right for you.

Data form

A data form (data form: A dialog box that displays one complete record at a time. You can use data forms to add, change, locate, and delete records.) provides a convenient way to enter or display one complete row of information in a range or table without scrolling horizontally. You may find that using a data form can make data entry easier than moving from column to column when you have more columns of data than can be viewed on the screen. Use a data form when a simple form of text boxes that list the column headings as labels is sufficient and you don't need sophisticated or custom form features, such as a list box or spin button.
Example of a typical data form Excel can automatically generate a built-in data form for your range (range: Two or more cells on a sheet. The cells in a range can be adjacent or nonadjacent.) or table (table: A collection of data about a particular subject that is stored in records (rows) and fields (columns).). The data form displays all column headers as labels in a single dialog box. Each label has an adjacent blank text box in which you can enter data for each column, up to a maximum of 32 columns. In a data form, you can enter new rows, find rows by navigating, or (based on cell contents) update rows and delete rows . If a cell contains a formula (formula: A sequence of values, cell references, names, functions, or operators in a cell that together produce a new value. A formula always begins with an equal sign (=).), the formula result is displayed in the data form, but you cannot change the formula by using the data form.

Worksheet with Form and ActiveX controls

A worksheet is a type of form that enables you to enter and view data on the grid, and there are several control-like features already built-in to Excel worksheets, such as comments and data validation. Cells resemble text boxes in that you can enter and format them in a variety of ways. Cells are often used as labels, and by adjusting cell height and width and merging cells, you can make a worksheet behave like a simple data entry form. Other control-like features, such as cell comments, hyperlinks, background images, data validation, conditional formatting, embedded charts, and AutoFilter can make a worksheet behave like an advanced form.
For added flexibility, you can add controls and other drawing objects to the drawing canvas (drawing canvas: An area on which you can draw multiple shapes. Because the shapes are contained within the drawing canvas, they can be moved and resized as a unit.) of a worksheet, and combine and coordinate them with worksheet cells. For example, you can use a list box control to make it easier for a user to select from a list of items. Or, you can use a spin button control to make it easier for a user to enter a number.
Because controls and objects are stored on the drawing canvas, you can display or view controls and objects alongside associated text that is independent of row and column boundaries without changing the layout of a grid or table of data on your worksheet. Most of the time, many of these controls can also be linked to cells on the worksheet and do not require VBA code to make them work. You can set properties that determine whether a control floats freely or moves and resizes together with a cell. For example, you might have a check box that you want to move together with its underlying cell when the range is sorted. However, if you have a list box that you want to keep in a specific location at all times, you probably do not want it to move together with its underlying cell.
Excel has two types of controls: Form controls and ActiveX Controls. In addition to these sets of controls, you can also add objects from the Drawing tools, such as a AutoShapes (AutoShapes: A group of ready-made shapes that includes basic shapes, such as rectangles and circles, plus a variety of lines and connectors, block arrows, flowchart symbols, stars and banners, and callouts.), WordArt (WordArt: Text objects you create with ready-made effects to which you can apply additional formatting options.), SmartArt graphic, or text boxes.
The following sections describe these controls and drawing objects, and also explain how to work with these controls and objects in more detail.

Form controls

Form controls are the original controls that are compatible with earlier versions of Excel, starting with Excel version 5.0. Form controls are also designed for use on XLM macro sheets.
You use Form controls when you want to easily reference and interact with cell data without using VBA code, and when you want to add controls to chart sheets (chart sheet: A sheet in a workbook that contains only a chart. A chart sheet is beneficial when you want to view a chart or a PivotChart report separately from worksheet data or a PivotTable report.). For example, after you add a list box control to a worksheet and linking it to a cell, you can return a numeric value for the current position of the selected item in the control. You can then use that numeric value in conjunction with the INDEX function to select different items from the list.
You can also run macros by using Form controls. You can attach an existing macro to a control, or write or record a new macro. When a user of the form clicks the control, the control runs the macro.
However, these controls cannot be added to UserForms, used to control events, or modified to run Web scripts on Web pages.
Summary of form controls
Button NameExampleDescription
Button image LabelExample of a Form label control Identifies the purpose of a cell or text box, or displays descriptive text (such as titles, captions, pictures) or brief instructions.
Button image Group boxExample of Forms toolbar group box control Groups related controls into one visual unit in a rectangle with an optional label. Typically, option buttons, check boxes, or closely related contents are grouped.
Button image ButtonExample of a Forms toolbar button control Runs a macro that performs an action when a user clicks it. A button is also referred to as a push button.
Button image Check boxExample of a Forms toolbar check box control Turns on or off a value that indicates an opposite and unambiguous choice. You can select more than one check box on a worksheet or in a group box. A check box can have one of three states: selected (turned on), cleared (turned off), and mixed, meaning a combination of on and off states (as in a multiple selection).
Button image Option buttonExample of a Forms toolbar option button control Allows a single choice within a limited set of mutually exclusive choices; an option button is usually contained in a group box or a frame. An option button can have one of three states: selected (turned on), cleared (turned off), and mixed, meaning a combination of on and off states (as in a multiple selection). An option button is also referred to as a radio button.
Button image List boxExample of a Form list box control Displays a list of one or more items of text from which a user can choose. Use a list box for displaying large numbers of choices that vary in number or content. There are three types of list boxes:
  • A single-selection list box enables only one choice. In this case, a list box resembles a group of option buttons, except that a list box can handle a large number of items more efficiently.
  • A multiple-selection list box enables either one choice or contiguous (adjacent) choices.
  • An extended-selection list box enables one choice, contiguous choices, and noncontiguous (or disjointed) choices.
Button image Combo boxExample of a Form combo box control Combines a text box with a list box to create a drop-down list box. A combo box is more compact than a list box but requires the user to click the down arrow to display the list of items. Use a combo box to enable a user to either type an entry or choose only one item from the list. The control displays the current value in the text box, regardless of how that value is entered.
Button image Scroll barExample of a Forms toolbar scroll bar control Scrolls through a range of values when you click the scroll arrows or drag the scroll box. In addition, you can move through a page (a preset interval) of values by clicking the area between the scroll box and either of the scroll arrows. Typically, a user can also type a text value directly into an associated cell or text box.
Button image Spin buttonExample of a Forms toolbar spinner control Increases or decreases a value, such as a number increment, time, or date. To increase the value, click the up arrow; to decrease the value, click the down arrow. Typically, a user can also type a text value directly into an associated cell or text box.
Note The following controls are unavailable in Office Excel 2007 workbooks. These controls can be used in Excel version 5.0 dialog sheets only.
Button Name
Button image Text Field
Button image Combo List - Edit
Button image Combo Drop - Down Edit
Button image Run Dialog
 

ActiveX controls

ActiveX controls (ActiveX control: A control, such as a check box or button that offers options to users or runs macros or scripts that automate a task. You can write macros for the control in Microsoft Visual Basic for Applications or scripts in Microsoft Script Editor.) can be used on worksheet forms, with or without the use of VBA code, and on VBA UserForms. In general, use ActiveX controls when you need more flexible design requirements than those provided by Form controls. ActiveX controls have extensive properties that you can use to customize their appearance, behavior, fonts, and other characteristics.
You can also control different events that occur when an ActiveX control is interacted with. For example, you can perform different actions, depending on which choice a user selects from a list box control, or you can query a database to refill a combo box with items when a user clicks a button. You can also write macros that respond to events associated with ActiveX controls. When a user of the form interacts with the control, your VBA code then runs to process any events that occur for that control.
Your computer also contains many ActiveX controls that were installed by Excel and other programs, such as Calendar Control 12.0 and Windows Media Player.
Important Not all ActiveX controls can be used directly on worksheets; some can be used only on Visual Basic for Applications (VBA) (Visual Basic for Applications (VBA): A macro-language version of Microsoft Visual Basic that is used to program Microsoft Windows-based applications and is included with several Microsoft programs.) UserForms. If you try to add any one of these particular ActiveX controls to a worksheet, Excel displays the message "Cannot insert object."
However, ActiveX controls cannot be added to chart sheets from the user interface or to XLM macro sheets. You also cannot assign a macro to run directly from an ActiveX control the same way you can from a Form control.
Summary of ActiveX controls
Button NameExampleDescription
Button image Check boxExample of an ActiveX check box control Turns on or off a value that indicates an opposite and unambiguous choice. You can select more than one check box at a time on a worksheet or in a group box. A check box can have one of three states: selected (turned on), cleared (turned off), and mixed, meaning a combination of on and off states (as in a multiple selection).
Button image Text boxExample of an ActiveX text box control Enables you to, in a rectangular box, view, type, or edit text or data that is bound to a cell. A text box can also be a static text field that presents read-only information.
Button image Command buttonExample of an ActiveX command button control Runs a macro that performs an action when a user clicks it. A command button is also referred to as a push button.
Button image Option buttonExample of an ActiveX option button control Allows a single choice within a limited set of mutually exclusive choices usually contained in a group box or frame. An option button can have one of three states: selected (turned on), cleared (turned off), and mixed, meaning a combination of on and off states (as in a multiple selection). An option button is also referred to as a radio button.
Button image List boxExample of an ActiveX list box control Displays a list of one or more items of text from which a user can choose. Use a list box for displaying large numbers of choices that vary in number or content. There are three types of list boxes:
  • A single-selection list box enables only one choice. In this case, a list box resembles a group of option buttons, except that a list box can handle a large number of items more efficiently.
  • A multiple selection list box enables either one choice or contiguous (adjacent) choices.
  • An extended-selection list box enables one choice, contiguous choices, and noncontiguous (or disjointed) choices.
Button image Combo boxExample of an ActiveX combo box control Combines a text box with a list box to create a drop-down list box. A combo box is more compact than a list box, but requires the user to click the down arrow to display the list of items. Use to allow a user to either type an entry or choose only one item from the list. The control displays the current value in the text box, regardless of how that value is entered.
Button image Toggle buttonExample of an ActiveX toggle button control Indicates a state, such as Yes/No, or a mode, such as On/Off. The button alternates between an enabled and disabled state when it is clicked.
Button image Spin buttonExample of an ActiveX spin button control Increases or decreases a value, such as a number increment, time, or date. To increase the value, click the up arrow; to decrease the value, click the down arrow. Typically, a user can also type a text value into an associated cell or text box.
Button image Scroll barExample of an ActiveX scroll bar control Scrolls through a range of values when you click the scroll arrows or drag the scroll box. In addition, you can move through a page (a preset interval) of values by clicking the area between the scroll box and either of the scroll arrows. Typically, a user can also type a text value directly into an associated cell or text box.
Button image LabelExample of an ActiveX label control Identifies the purpose of a cell or text box, displays descriptive text (such as titles, captions, pictures), or provides brief instructions.
Button image ImageExample of an ActiveX image control Embeds a picture, such as a bitmap (bitmap: A picture made from a series of small dots, much like a piece of graph paper with certain squares filled in to form shapes and lines. When stored as files, bitmaps usually have the extension .bmp.), JPEG (JPEG: A graphics file format (.jpg extension in Microsoft Windows) supported by many Web browsers that was developed for compressing and storing photographic images. It's best used for graphics with many colors, such as scanned photos.), or GIF (GIF: A graphics file format (.gif extension in Windows) used to display indexed-color graphics on the World Wide Web. It supports up to 256 colors and uses lossless compression, meaning that no image data is lost when the file is compressed.).
Frame control Example of an ActiveX group box control A rectangular object with an optional label that groups related controls into one visual unit. Typically, option buttons, check boxes, or closely related contents are grouped in a frame control.
Note The ActiveX frame control is not available in the ActiveX Controls section of the Insert command. However, you can add the control from the More Controls dialog box by selecting Microsoft Forms 2.0 Frame.
Button image More Controls Displays a list of additional ActiveX controls available on your computer that you can add to a custom form, such as Calendar Control 12.0 and Windows Media Player. You can also register a custom control in this dialog box.
 

Drawing Tool objects

You may also want to include SmartArt graphics, Shapes, WordArt, and text boxes on your form. You can resize, rotate, flip, color, and combine these objects to create even more complex shapes. When you type text directly in a Shape or text box object, the text becomes part of that object — if you rotate or flip the object, the text rotates or flips with it. Unlike ActiveX controls, you can assign different attributes, such as font size and font style, to individual words and characters in the object. You can also assign macros and add hyperlinks to these objects. You can even link text in a Shape or text box object to a worksheet cell and dynamically display updated values in those objects.

Working with controls and objects on the worksheet form

After adding forms and ActiveX to a worksheet form, you usually want to fine-tune and rearrange the controls in a variety of ways to create a well-designed, user friendly form. Common tasks include the following:
  • Controlling the display of gridlines while you work with the controls, and deciding whether to display the gridlines to the user on the final worksheet form.
  • Selecting and deselecting controls so that you can specify properties or make additional adjustments.
  • Editing text in a control, such as the caption or label.
  • Grouping, copying, moving, and aligning controls to organize the layout of the worksheet form.
  • Resizing and formatting controls to obtain the appearance that you want.
  • Positioning or sizing a control with a cell.
  • Protecting controls and linked cells according to your specific data protection needs.
  • Enabling or disabling the printing of controls when the worksheet form is printed.
  • Deleting unused controls.
You can design a worksheet form with or without cell gridlines in the background. For example, you might want to turn off cell gridlines and then format all the cells with the same color or pattern, or even use a picture as a sheet background. To hide or show the gridlines, on the View tab, in the Show/Hide group, clear or select the Gridlines check box.
For more information, see the See Also section of this topic.

Determining the type of control that is on your worksheet

Because there are three different types of controls and objects that you can modify uniquely, you might not know for sure which type of control it is just by looking at it. To determine the type of control (Form or ActiveX), select and right-click the control, and then display the shortcut menu:
  • If the shortcut menu contains the command Properties, the control is an ActiveX control, and you are in design mode.
  • If the shortcut menu contains the command Assign Macro, the control is a Form control.
Tip To display the correct shortcut menu for the group box Form control, make sure that you select the perimeter instead of the interior of the group box.
  • If the shortcut menu contains the command Edit Text, the object is a Drawing object.