Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to use VBA to remove values from a Listbox

Posted on 2011-09-08
3
Medium Priority
?
438 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
TinTombStone earned 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

705 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