Microsoft KB Archive/182696

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

Article ID: 182696

Article Last Modified on 10/10/2006

-

APPLIES TO


 * Microsoft Excel 98 for Macintosh

-



This article was previously published under Q182696



SUMMARY
This article illustrates how to remove all items from a ListBox or ComboBox control.



MORE INFORMATION
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. To build a sample UserForm that contains a ListBox control that is populated when the UserForm loads and then removes 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 OPTION+F11). On the Insert menu, click UserForm. Double-click the UserForm to open the Code window for the UserForm.  In the module, type the following code for the UserForm Initialize event: Private Sub UserForm_Initialize

' Populate the ListBox. ListBox1.List = Worksheets("Sheet1").Range("A1:A5").Value End Sub

This procedure populates ListBox1 when the UserForm is loaded.  Draw a ListBox control on the UserForm.</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 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> Run the UserForm.</li> Click the CommandButton.</li></ol>

All of the items are removed from ListBox1.

<div class="references_section">