Microsoft KB Archive/213721

= XL2000: How to Remove All Items from a ListBox or ComboBox =

Article ID: 213721

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Excel 2000 Standard Edition

-



This article was previously published under Q213721





SUMMARY
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.



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 a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

https://partner.microsoft.com/global/30000104

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

ListBox or ComboBox Control That Is Bound to a Worksheet
To build a sample UserForm that contains a ListBox control that is bound to a worksheet and then remove items in the control, follow these steps:  Close and save any open workbooks, and then create a new workbook.  On Sheet1, type the following values:       A1: Alpha A2: Bravo A3: Charlie A4: Delta A5: Echo  Start the Visual Basic Editor (press ALT+F11). If the Properties window is not visible, click Properties on the View menu (or press F4). If the Project Explorer window is not visible, click Project Explorer on the View menu. On the Insert menu, click UserForm.</li> Draw a ListBox control on the UserForm.</li> Switch to the Properties window (press F4).</li> Change the RowSource property of the ListBox control to the following value:

Sheet1!A1:A5

</li> Draw a CommandButton control on the UserForm.</li> Double-click the CommandButton to open the code window for the CommandButton.</li>  In the module, type the following code for the CommandButton Click event: Private Sub CommandButton1_Click ListBox1.RowSource = "" End Sub </li> Run the UserForm.

The list box that you added to the UserForm is populated with the values that you entered on Sheet1.</li> Click the CommandButton.</li></ol>

All of the items are removed from ListBox1.

ListBox or ComboBox That Is Not Bound to a Worksheet
To build a sample UserForm that contains a ListBox control that is populated with an array of values when the UserForm loads, and to then remove items in the control, follow these steps: <ol> Close and save any open workbooks, and then create a new workbook.</li> Start the Visual Basic Editor (press ALT+F11).</li> If the Properties window is not visible, click Properties on the View menu (or press F4).</li> On the Insert menu, click UserForm.</li> Double-click the UserForm to open the code window for the UserForm.</li>  In the module, type the following code for the UserForm Initialize event: 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. </li> Draw a ListBox control on the UserForm.</li> <li>Draw a CommandButton control on the UserForm.</li> <li>Double-click the CommandButton to open the code window for the CommandButton.</li> <li> In the module, type the following code for the CommandButton Click event: 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. </li> <li>Run the UserForm.</li> <li>Click the CommandButton.</li></ol>

All of the items are removed from ListBox1.

<div class="references_section">