Solved

How to use VBA to remove values from a Listbox

Posted on 2011-09-08
3
425 Views
Last Modified: 2012-05-12
I am trying to remove all values from a Listbox in a Worksheet (not a Userform).
I can use this code to de-select all values, but I haven't been able to find a way to actually remove all items.  I get a "method not supported by this object" if I try to use .Clear or RowSource = ""


Sub ListClear()

    Dim ws As Worksheet
    Dim LB As MSForms.ListBox
    Dim lCount As Long
   
    Set ws = ActiveSheet
    Set LB = ws.OLEObjects("ListBox2").Object
   
    With LB
        For lCount = 0 To .ListCount - 1
           .Items(lCount) = False
        Next
    End With
0
Comment
Question by:Ed_CLP
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
TinTombStone earned 250 total points
ID: 36503067
This worked for me

the AddItems are just to get something into the box
Sub ListClear()
Dim ws As Worksheet
    Dim LB As MSForms.ListBox
    Dim lCount As Long
    
    Set ws = ActiveSheet
    Set LB = ws.OLEObjects("ListBox2").Object
    
    LB.AddItem "Item 1"
    LB.AddItem "Item 2"
    LB.AddItem "Item 3"
    LB.AddItem "Item 4"
    LB.AddItem "Item 5"
   
    LB.Clear
 

End Sub

Open in new window

0
 
LVL 6

Expert Comment

by:TinTombStone
ID: 36503099
Or using your method


Sub ListClear()
Dim ws As Worksheet
    Dim LB As MSForms.ListBox
    Dim lCount As Long
    
    Set ws = ActiveSheet
    Set LB = ws.OLEObjects("ListBox2").Object
    
    LB.AddItem "Item 1"
    LB.AddItem "Item 2"
    LB.AddItem "Item 3"
    LB.AddItem "Item 4"
    LB.AddItem "Item 5"
   
    With LB
        For lCount = .ListCount - 1 To 0 Step -1
           LB.RemoveItem lCount
        Next
    End With

End Sub

Open in new window

0
 

Author Comment

by:Ed_CLP
ID: 36503259
I found the problem - The ListFillrange property is set to use a named range.  This apparently prevents you from changing any of the list values.  If I disable this property, your code works fine.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many companies are making the switch from Microsoft to Google Apps (https://www.google.com/work/apps/business/). Use this article to learn more about what Google Apps has to offer and to help if you’re planning on migrating to Google Apps. It is …
The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question