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

Last reviewed: March 13, 1998
Article ID: Q165632
The information in this article applies to:
  • Microsoft Excel 97 for Windows

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 examples of Visual Basic for Applications procedures 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. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

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, use the following 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 ALT+F11).

  4. If the Properties window is not visible, click Properties on the View menu (or press F4).

  5. If the Project Explorer window is not visible, click Project Explorer on the View menu.

  6. On the Insert menu, click UserForm.

  7. Draw a ListBox control on the UserForm.

  8. Switch to the Properties window (press F4).

  9. Change the RowSource property of the ListBox control to the following value:

    Sheet1!A1:A5

  10. Draw a CommandButton control on the UserForm.

  11. Double-click the CommandButton to open the code window for the CommandButton.

  12. In the module, type the following code for the CommandButton Click event:

    Private Sub CommandButton1_Click()

               ListBox1.RowSource = ""
           End Sub
    
    

  13. Run the UserForm.

    The list box that you added to the UserForm is populated with the values that you entered on Sheet1.

  14. Click the CommandButton.

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 then remove items in the control, use the following steps:

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

  2. Start the Visual Basic Editor (press ALT+F11).

  3. If the Properties window is not visible, click Properties on the View menu (or press F4).

  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()

               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.
    
    

  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 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:

   ARTICLE-ID: Q120802
   TITLE     : Office: How to Add/Remove a Single Office
               Program or Component


Additional query words: XL97
Keywords : kbprg kbui
Version : WINDOWS:97
Platform : WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 13, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.