Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to use VBA to remove values from a Listbox

Posted on 2011-09-08
3
Medium Priority
?
440 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 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
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 demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

972 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