Solved

How to use VBA to remove values from a Listbox

Posted on 2011-09-08
3
431 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 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
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…

624 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