How to add data to a ComboBox or a ListBox in Excel for Mac (185388)



The information in this article applies to:

  • Microsoft Excel 2004 for Mac
  • Microsoft Excel X for Mac
  • Microsoft Excel 2001 for Mac
  • Microsoft Excel 98 Macintosh Edition

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:
  1. Create a new workbook in Microsoft Excel for Mac.
  2. Press OPTION+F11 to start the Visual Basic Editor.
  3. On the Insert menu, click UserForm.
  4. Create a ListBox control and two CommandButton controls on the user form.
  5. Select the first command button. Press F6 to view the Properties window. Type Enter a New Item in the Caption property box.
  6. Select the second command button. Press F6 to view the Properties window. Type Close in the Caption property box.
  7. Double-click the user form. The Code window for the user form appears.
  8. 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
    					
  9. On the Run menu, click Run Sub/UserForm to run the user form.
  10. Click Enter a New Item. Type Test in the input box that appears. Click OK.
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:
  1. Start Excel for Mac, and create a new workbook.
  2. Type the following data in Sheet1 of the new workbook:
          A1: Apples
          A2: Pears
          A3: Bananas
    					
  3. Press OPTION+F11 to start the Visual Basic Editor.
  4. On the Insert menu, click UserForm.
  5. Create a ComboBox control and two CommandButton controls on the user form.
  6. Select the first command button. Press F6 to view the Properties window. Type Enter Data in the Caption property box.
  7. Select the second command button. Press F6 to view the Properties window. Type Close in the Caption property box.
  8. Double-click the user form. The Code window for the user form appears.
  9. 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
    					
  10. On the Run menu, click Run Sub/UserForm to run the user form.
  11. When the user form appears, click Enter Data.
  12. Click the drop-down arrow for the combo box.
You can now select Apples, Pears, or Bananas from the combo box. To close the user form, click Close.

REFERENCES

For more information about user forms, in the Visual Basic Editor, click the Office Assistant, type UserForms, and then click Search to view the topics returned.

Modification Type:MajorLast Reviewed:6/17/2005
Keywords:kbhowto kbProgramming KB185388