Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:
Microsoft Certified Partners -
https://partner.microsoft.com/global/30000104Microsoft Advisory Services -
http://support.microsoft.com/gp/advisoryserviceFor more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:
http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS
A
RefEdit control is similar in behavior and appearance to an edit box control with an
InputType property that you set to
xlReference. (The edit box control is available in Microsoft Excel 5.0 or 7.0 on the
Forms toolbar in dialog sheets.) The
RefEdit control differs from an edit box control in that the
RefEdit control contains a button that allows you to collapse the UserForm to easily select a cell range.
Using an Edit Box
The
RefEdit control imitates the behavior of the reference edit boxes that are built into Excel. To illustrate how the built-in reference
edit boxes work in Excel, follow these steps:
- In a new worksheet, on the Insert menu, click Function to display the Function Wizard.
- Click SUM in the Function Name box, and then click OK.
Notice that the edit box for the first argument contains a button with a
red arrow on the right side.
- Click the button with the red arrow in the edit box.
The Function Wizard collapses, which allows you to see more of your
worksheet.
- Drag the pointer over cells in the worksheet.
The reference for the cells appears in the Formula Palette box. - Click the button in the Formula Palette box to expand the Function Wizard.
- Click OK or Cancel.
The
RefEdit control that you can use with your Excel Visual Basic
project is an ActiveX control. The file name for the
RefEdit control is Refedit.dll.
Using the RefEdit Control on a UserForm
The following example illustrates how to create a UserForm that contains a
RefEdit control and a
CommandButton control. When the UserForm appears, you type a reference in the
RefEdit control and click
OK. When you click
OK, the range that you referenced is formatted with a red background, and the UserForm is unloaded.
To see how the
RefEdit control works in Excel, follow these steps:
- In a new workbook, press ALT+F11 to start the Microsoft Visual Basic Editor.
- On the Insert menu, click UserForm.
This step inserts UserForm1 into your project. If the Toolbox is not displayed, click Toolbox on the View menu. - In the Toolbox dialog box, click RefEdit, and then draw the control on UserForm1.
To determine which button is the RefEdit button, move the pointer over each button and examine the name that appears in the ToolTip.
NOTE: If there is no RefEdit button in the ToolBox, follow these steps:- On the Tools menu, click Additional Controls.
- In the Additional Controls dialog box, click to select the RefEdit.ctrl check box, and then click OK.
- In the Toolbox dialog box, click CommandButton, and then draw the command button on UserForm1.
- With the command button selected, press F4 to display the Properties
window. Change the Caption property of the control to OK.
- Right-click CommandButton, and then click View Code.
- Type the following code for the Click event of the CommandButton:
Sub CommandButton1_Click()
Dim SelRange as Range
Dim Addr as String
'Get the address, or reference, from the RefEdit control.
Addr = RefEdit1.Value
'Set the SelRange Range object to the range specified in the
'RefEdit control.
Set SelRange = Range(Addr)
'Apply a red pattern to the SelRange.
SelRange.Interior.ColorIndex = 3
'Unload the userform.
Unload Me
End Sub
- Press F5 to run the UserForm.
The UserForm is displayed. - Click the button in the RefEdit control.
Notice that the UserForm collapses. - Select a range of cells on the worksheet, and then click the RefEdit button to expand the UserForm.
- Click OK.
The UserForm disappears, and the cells that you selected are formatted
with a red background.