This article provides examples of how to use a Microsoft Visual Basic for Applications macro to populate a
ListBox or
ComboBox control.
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.
- Double-click the user form. The Code window for the user form 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 user form.
- 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:
- Start Excel for Mac, and create a new workbook.
- Type the following data in 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 control and two CommandButton controls on the user form.
- 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 user form. The Code window for the user form 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 user form.
- When the user form appears, click Enter Data.
- 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.
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.