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

ID: Q182696

The information in this article applies to:

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, 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/refguide/

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:

  1. Close and save any open workbooks, and then create a new workbook.

  2. On Sheet1, type the following values:

       A1: Alpha
       A2: Bravo
       A3: Charlie
       A4: Delta
       A5: Echo

  3. Start the Visual Basic Editor (press OPTION+F11).

  4. On the Insert menu, click UserForm.

  5. Double-click the UserForm to open the Code window for the UserForm.

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

  7. Draw a ListBox control on the UserForm.

  8. Draw a CommandButton control on the UserForm.

  9. Double-click the CommandButton to open the Code window for the
     CommandButton.

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

 11. Run the UserForm.

 12. Click the CommandButton.

All of the items are removed from ListBox1.

REFERENCES

For more information about using the ListBox control, click the Office Assistant in the Visual Basic Editor, type "listbox control" (without the quotation marks), click Search, and then click to view the "ListBox Control" topic.

For more information about using the RemoveItem method, click the Office Assistant in the Visual Basic Editor, type "removeitem" (without the 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 Help is not installed on your computer, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q179216
   TITLE     : OFF98: How to Use the Microsoft Office Installer Program

Additional query words: XL98
Keywords          : kbprg kbdta kbdtacode xlvbahowto xlvbainf 
Version           : MACINTOSH:98
Platform          : MACINTOSH
Issue type        : kbhowto

Last Reviewed: May 18, 1999