+ Reply to Thread
Results 1 to 3 of 3

Thread: Removing all items from a list box

  1. #1
    Registered User
    Join Date
    11-13-2009
    Location
    Cumbria
    MS-Off Ver
    Excel 2007
    Posts
    44

    Removing all items from a list box

    This is my attempt at removing all items from a worksheet listbox active X control that doesn't work. I'm probably not even along the right lines. This might not make any sense:

    
    For i = 0 To ws.OLEObjects("ListBox1").Object.ListCount - 1
    
    ws.OLEObjects("ListBox1").Object.RemoveItem ws.OLEObjects("ListBox1").Object.List(i)
    
    Next i
    ws.OLEObjects("ListBox1") is definitely the correct way of referencing the list box if that helps you identify what type of list box it is that I'm using.

    Many thanks in advance for any help that can be offered,

    Rowan
    Last edited by RowanB; 01-27-2012 at 03:46 AM.

  2. #2
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    359

    Re: Removing all items from a list box

    The following code which would be inside the sheet containing the listbox will add some items then the second macro will clear the listbox
    Sub lbControl()
    For k = 1 To 10
    ListBox1.AddItem "Hello there number " & CStr(k)
    Next
    End Sub
    
    Sub lbControlRm()
    'to remove just one item from the listbox
    ListBox1.RemoveItem (2) ' where 2 is the index number which starts at 0
    'or to clear the entire listbox
    ListBox1.Clear
    End Sub
    If you are doing this from a module which is what I assume you are doing you need to (as you have done) reference the listbox
    Sub lbControl()
    Set ws = Worksheets("Sheet1")
    For k = 1 To 10
    ws.ListBox1.AddItem "Hello there number " & CStr(k)
    Next
    End Sub
    
    Sub lbControlRm()
    Set ws = Worksheets("Sheet1")
    'to remove just one item from the listbox
    ws.ListBox1.RemoveItem (2)
    'or to clear the entire listbox
    ws.ListBox1.Clear
    End Sub
    Hope this helps.
    Anthony
    “Confidence never comes from having all the answers; it comes from being open to all the questions.”
    PS: Remember to mark your questions as Solved once you are satisfied and rate the answer(s) questions.”
    Last edited by smuzoen; 01-26-2012 at 07:39 PM.

  3. #3
    Registered User
    Join Date
    11-13-2009
    Location
    Cumbria
    MS-Off Ver
    Excel 2007
    Posts
    44

    Re: Removing all items from a list box

    Thanks a lot. That's great.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0