XL98: How to Add Data to a ComboBox or a ListBox
ID: Q185388
|
The information in this article applies to:
-
Microsoft Excel 98 Macintosh Edition
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/overview/overview.asp
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:
- Create a new workbook in Microsoft Excel.
- Press OPTION+F11 to start the Visual Basic Editor.
- On the Insert menu, click UserForm.
- Create a ListBox control and two Command Buttons on the UserForm.
- 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.
- Double-click the UserForm. The Code window for the UserForm appears.
- 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
- On the Run menu, click Run Sub/UserForm to run the UserForm.
- Click "Enter a New Item." Type Test 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:
- Start Microsoft Excel 98 with a new workbook.
- Type the following into Sheet1 of the new workbook:
A1: Apples
A2: Pears
A3: Bananas
- Press OPTION+F11 to start the Visual Basic Editor.
- On the Insert menu, click UserForm.
- Create a ComboBox and two Command Buttons on the UserForm.
- Select the first Command Button. Press F6 to view the properties
window. Type Enter Data in the Caption
property box.
- Select the second Command Button. Press F6 to view the properties
window. Type Close in the Caption
property box.
- Double-click the UserForm. The Code window for the UserForm appears.
- 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
- On the Run menu, click Run Sub/UserForm to run the UserForm.
- When the UserForm appears, click Enter Data.
- 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:
Q179216
OFF98: How to Use the Microsoft Office Installer Program
Additional query words:
XL98 user form combo box list
Keywords : kbprg kbdta EPUCon
Version : MACINTOSH:98
Platform : MACINTOSH
Issue type : kbhowto
|