Microsoft KB Archive/161598

= How to add data to a ComboBox control or a ListBox control in Excel or Word =

Article ID: 161598

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Excel 2002 Standard Edition
 * Microsoft Word 2002 Standard Edition
 * Microsoft Excel 2000 Standard Edition
 * Microsoft Word 2000 Standard Edition
 * Microsoft Excel 97 Standard Edition
 * Microsoft Word 97 Standard Edition

-



This article was previously published under Q161598





For a Microsoft Excel for Mac version of this article, see 185388.



SUMMARY
This article describes methods of populating a ComboBox or ListBox control.



MORE INFORMATION
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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

https://partner.microsoft.com/global/30000104

For 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

You can use a control, such as a combo box or a list box, on a user form. In Microsoft Excel, you can use these controls in the worksheet. The methods for populating the controls are similar. The examples in this article use controls on a user form.

Method 1: How to Link a Combo Box or List Box to Cells on an Excel Worksheet
  Create a new workbook in Microsoft Excel. On Sheet1, type the following:      A1: One     B1: Red A2: Two    B2: Green A3: Three  B3: Yellow A4: Four   B4: Blue  Press ALT+F11 to start the Microsoft Excel Visual Basic Editor. On the Insert menu, click UserForm. Create a ComboBox control and a ListBox control on the user form. If the Properties window is not visible, click Properties Window on the View menu.</li> Click the ComboBox and scroll to RowSource in the Properties window.

The window is divided into two sections. The left section contains the name of the property for the control. The right section contains the value of the property. Click in the right section of RowSource and type "sheet1!a1:a4" (without the quotation marks).</li> Click the ListBox and scroll to RowSource in the Properties window. Click in the right section of RowSource and type "sheet1!b1:b4" (without the quotation marks).

NOTE: If you omit the sheet reference, the ComboBox and ListBox controls are populated with the data from the cells from the worksheet that is active when you run the user form. To ensure that the data from the same worksheet is always used to populate the user form, include the sheet reference as shown in the examples. To populate the user form with data from the active sheet, do not include the sheet reference in the RowSource. When you do this, the macro uses data from the active sheet to populate the user form.</li> Run the user form by clicking Run and then clicking Run Sub/UserForm.</li></ol>

One, Two, Three, and Four appear in the ComboBox control; and Red, Green, Yellow, and Blue appear in the ListBox control.

Methdod 2: How to Use a Visual Basic Macro to Populate a Combo Box or List Box
<ol> Open a Microsoft Office Application such as Microsoft Excel or Word.</li> Press ALT+F11 to start the Visual Basic Editor.</li> On the Insert menu, click UserForm.</li> Create a ComboBox and a ListBox control on the user form.</li> Right-click the user form and click View Code. The Code window for the user form appears. Click Initialize in the Procedure list (on the right side).

The first and last lines of the subroutine are automatically entered, and the insertion point is automatically placed between these two lines.</li>  Type the following code: ListBox1.List = Array("One", "Two", "Three", "Four") ComboBox1.List = Array("Red", "Green", "Yellow", "Blue") This code should appear between the lines "Sub UserForm_Initialize" and "End Sub". </li> On the Run menu, click Run Sub/UserForm to run the user form.</li></ol>

One, Two, Three, and Four appear in the ListBox control; and Red, Green, Yellow, and Blue appear in the ComboBox control.

<div class="references_section">