Microsoft KB Archive/185388

= How to add data to a ComboBox or a ListBox in Excel for Mac =

Article ID: 185388

Article Last Modified on 1/23/2007

-

APPLIES TO


 * Microsoft Excel 2004 for Mac
 * Microsoft Excel X for Mac
 * Microsoft Excel 2001 for Mac
 * Microsoft Excel 98 for Macintosh

-



This article was previously published under Q185388





SUMMARY
This article provides examples of how to use a Microsoft Visual Basic for Applications macro to populate a ListBox or ComboBox control.



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements.

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

How to Use an Input Box and the AddItem Method to Populate a List Box
NOTE: You can also use this example with a combo box.

To populate a ListBox control by using an input box and the AddItem method, follow these steps:  Create a new workbook in Microsoft Excel for Mac. Press OPTION+F11 to start the Visual Basic Editor. On the Insert menu, click UserForm. Create a ListBox control and two CommandButton controls on the user form. Select the first command button. Press F6 to view the Properties window. Type Enter a New Item in the Caption property box. Select the second command button. Press F6 to view the Properties window. Type Close in the Caption property box.</li> Double-click the user form. The Code window for the user form appears.</li>  Type the following code: Private Sub CommandButton1_Click Dim x As Variant

x = InputBox _ ("Type Data To Be Placed Into The ListBox and Click OK")

ListBox1.AddItem x End Sub

Private Sub CommandButton2_Click ' Close the UserForm. Unload Me End Sub </li> On the Run menu, click Run Sub/UserForm to run the user form.</li> Click Enter a New Item. Type Test in the input box that appears. Click OK.</li></ol>

Test is added to the list in the list box on the user form. To close the user form, click Close.

How to Use the Column Property to Add Items to a Combo Box
NOTE: You can also use this example with a list box.

To populate a combo box using the Column property, follow these steps: <ol> Start Excel for Mac, and create a new workbook.</li>  Type the following data in Sheet1 of the new workbook: <pre class="fixed_text">     A1: Apples A2: Pears A3: Bananas </li> Press OPTION+F11 to start the Visual Basic Editor.</li> On the Insert menu, click UserForm.</li> Create a ComboBox control and two CommandButton controls on the user form.</li> Select the first command button. Press F6 to view the Properties window. Type Enter Data in the Caption property box.</li> Select the second command button. Press F6 to view the Properties window. Type Close in the Caption property box.</li> Double-click the user form. The Code window for the user form appears.</li>  Type the following code: Private Sub CommandButton1_Click Dim MyArray(0, 2) As String

MyArray(0, 0) = Worksheets(1).Range("A1").Value MyArray(0, 1) = Worksheets(1).Range("A2").Value MyArray(0, 2) = Worksheets(1).Range("A3").Value

ComboBox1.Column = MyArray End Sub

Private Sub CommandButton2_Click ' Close the UserForm. Unload Me End Sub </li> On the Run menu, click Run Sub/UserForm to run the user form.</li> When the user form appears, click Enter Data.</li> Click the drop-down arrow for the combo box.</li></ol>

You can now select Apples, Pears, or Bananas from the combo box. To close the user form, click Close.

<div class="references_section">