Excel 2007 Power Programming with VBA (Mr. Spreadsheets Bookshelf)

Many of Excel's built-in dialog boxes allow the user to specify a range. For example, the Goal Seek dialog box asks the user to select two single- cell ranges. The user can either type the range names directly or use the mouse to point and click in a sheet to make a range selection.

Your UserForms can also provide this type of functionality, thanks to the RefEdit control. The RefEdit control doesn't look exactly like the range selection control used in Excel's built-in dialog boxes, but it works in a similar manner. If the user clicks the small button on the right side of the control, the dialog box disappears temporarily, and a small range selector is displayed - which is exactly what happens with Excel's built-in dialog boxes.

Note  

Unfortunately, the RefEdit control has a few quirks that still haven't been fixed. You'll find that this control does not allow the user to use shortcut range selection keys (for example, pressing End, followed by Shift+ “ will not select cells to the end of the column). In addition, after clicking the small button on the right side of the control (to temporarily hide the dialog box), you're limited to mouse selections only. The keyboard can't be used at all to make a selection.

Figure 14-3 shows a UserForm that contains a RefEdit control. This dialog box enables the user to perform a simple mathematical operation on all nonformula (and non-empty) cells in the selected range. The operation that's performed corresponds to the selected OptionButton.

Figure 14-3: The RefEdit control shown here allows the user to select a range.

CD-ROM  

This example is available on the companion CD-ROM in a file named  range selection demo.xlsm .

Following are a few things to keep in mind when using a RefEdit control:

CROSS-REFERENCE  

As I discuss in Chapter 12, you can also use Excel's InputBox method to allow the user to select a range.

Категории