XL97: How to Remove All Items from a ListBox or ComboBoxID: Q165632
|
There is no single method that you can use to remove all items from a ListBox or ComboBox control on a UserForm. The method that you use to remove an item depends on whether the ListBox or ComboBox control is bound to a worksheet. This article contains examples that remove items from a sample control that is bound to a worksheet and a sample control that is not bound to a worksheet.
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 a Microsoft Certified Solution Provider
or the Microsoft fee-based consulting line at (800) 936-5200. For more information about
Microsoft Certified Solution Providers, please see the following page on the World Wide Web:
http://www.microsoft.com/mcsp/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
A1: Alpha
A2: Bravo
A3: Charlie
A4: Delta
A5: Echo
Sheet1!A1:A5
Private Sub CommandButton1_Click()
ListBox1.RowSource = ""
End Sub
Private Sub UserForm_Initialize()
Dim MyArray As Variant
Dim i As Integer
'Initialize array with values to populate ListBox.
MyArray = Array("Alpha", "Bravo", "Charlie", "Delta", "Echo")
For i = LBound(MyArray) To Ubound(MyArray)
'Add a value from MyArray to ListBox1.
UserForm1.ListBox1.AddItem MyArray(i)
Next
End Sub
This procedure populates ListBox1 when the UserForm is loaded.
Private Sub CommandButton1_Click()
Dim i As Integer
For i = 1 To ListBox1.ListCount
'Remove an item from the ListBox.
ListBox1.RemoveItem 0
Next i
End Sub
This Visual Basic procedure removes all of the items from ListBox1.
For more information about using the ListBox control, click the Office
Assistant in the Visual Basic Editor, type "listbox control" (without
quotation marks), click Search, and then click to view the "ListBox
Control" topic.
For more information about using the RowSource property, click the Office
Assistant in the Visual Basic Editor, type "rowsource" (without quotation
marks), click Search, and then click to view the "RowSource Property"
topic.
For more information about using the RemoveItem method, click the Office
Assistant in the Visual Basic Editor, type "removeitem" (without quotation
marks), click Search, and then click to view the "RemoveItem Method (VBA
Forms 97)" topic.
NOTE: If the Assistant is hidden, click the Office Assistant button on the
Standard toolbar. If Microsoft Excel Help is not installed on your
computer, please see the following article in the Microsoft Knowledge Base:
Q120802 Office: How to Add/Remove a Single Office Program or Component
Additional query words: XL97 user form
Keywords : kbprg kbui kbdta KbVBA
Version : WINDOWS:97
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: July 23, 1999