XL98: How to Add Data to a ComboBox or a ListBox

ID: Q185388

The information in this article applies to:

SUMMARY

In Microsoft Excel 98 Macintosh Edition, you can populate a ComboBox or ListBox control with a Microsoft Visual Basic for Applications macro. This article provides examples of different ways to populate a ListBox or ComboBox.

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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/

You can use a control, such as a ComboBox or a ListBox, on a worksheet or on a UserForm. The methods for populating the controls are similar. The examples in this article use controls on a UserForm.

Using an Input Box and the AddItem Method to Populate a ListBox

NOTE: You can also use this example with a ComboBox.

To populate a ListBox control using an Input Box and the AddItem method, follow these steps:

 1. Create a new workbook in Microsoft Excel.

 2. Press OPTION+F11 to start the Visual Basic Editor.

 3. On the Insert menu, click UserForm.

 4. Create a ListBox control and two Command Buttons on the UserForm.

 5. Select the first Command Button. Press F6 to view the properties
    window. Type "Enter a New Item" (without the quotation marks) in the
    Caption property box.

 6. Select the second Command Button. Press F6 to view the properties
    window. Type "Close" (without the quotation marks) in the Caption
    property box.

 7. Double-click the UserForm. The Code window for the UserForm 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 UserForm.

10. Click "Enter a New Item." Type "Test" (without the quotation marks) in
    the Input Box that appears. Click OK.

Test is added to the list in the ListBox on the UserForm. To close the UserForm, click Close.

Using the Column Property to Add Items to a ComboBox

NOTE: You can also use this example with a ListBox.

To populate a ComboBox using the Column property, follow these steps:

 1. Start Microsoft Excel 98 with a new workbook.

 2. Type the following into 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 and two Command Buttons on the UserForm.

 6. Select the first Command Button. Press F6 to view the properties
    window. Type "Enter Data" (without the quotation marks) in the Caption
    property box.

 7. Select the second Command Button. Press F6 to view the properties
    window. Type "Close" (without the quotation marks) in the Caption
    property box.

 8. Double-click the UserForm. The Code window for the UserForm 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 UserForm.

11. When the UserForm appears, click Enter Data.

12. Click the drop-down arrow for the ComboBox.

You can now select apples, pears, or bananas from the ComboBox. To close the UserForm, click Close.

REFERENCES

For more information about UserForms, click the Office Assistant while in the Visual Basic Editor, type "UserForms," click Search, and then click to view "UserForm Object, UserForm Collection."

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If Microsoft Help is not installed on your computer, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q179216
   TITLE     : OFF98: How to Use the Microsoft Office Installer Program

Additional query words: XL98 user form combo box list box
Keywords          : kbprg kbdta EPUCon 
Version           : MACINTOSH:98
Platform          : MACINTOSH
Issue type        : kbhowto

Last Reviewed: May 18, 1999