+ Reply to Thread
Results 1 to 13 of 13

How to Clear Listbox Form Control

  1. #1
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    How to Clear Listbox Form Control

    Hey all,

    I have the following code
    Please Login or Register  to view this content.
    You'll notice that .Clear is commented out. I would like to be able to clear this listbox, sort of like a refresh but I keep getting an error
    Object doesn't support property or method
    How can I perform a clear.

    Also, if snb is watching, how do you not use .AddItem in a situation like this where the list box is a form control / shape.
    Last edited by Mordred; 08-25-2011 at 10:02 AM.
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: How to Clear Listbox Form Control

    Mordred,

    For an ActiveX control, its best to treat it as an OLEObject. There is no .Clear (that I know of) for an ActiveX listbox, but the list is from the ListFillRange and you can set that to vbnullstring. Also, if you have the list on a worksheet somewhere, you can set up a named range and then set the listfillrange to that named range. See attached workbook for an example. There's a module with the following sub:
    Please Login or Register  to view this content.


    Hope that helps,
    ~tigeravatar
    Attached Files Attached Files

  3. #3
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: How to Clear Listbox Form Control

    Hi tigeravatar, thanks for your response. I tried your procedure but it throws an error when setting lb. I think this is because it is not an ActiveX control but a form control. In your workbook that you made up (thanks for that) you are using an ActiveX list box but I have had problems with them resizing on me so that is why I don't use them on a sheet. Can your code be tweaked to do the same with a form control list box?

  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: How to Clear Listbox Form Control

    Mordred,

    Updated code and attachment. Form controls are not OLEObjects and are treated as their respective control types. You'll notice that lb is dimmed as a listbox instead of an oleobject this time. Other than that, they're basically identical:
    Please Login or Register  to view this content.


    Hope that helps,
    ~tigeravatar
    Attached Files Attached Files

  5. #5
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: How to Clear Listbox Form Control

    Got ahead of myself, hehe. The form control listbox does have a .List property which you can use instead of the .ListFillRange. It'll do the same thing, but saves some typing:
    Please Login or Register  to view this content.

  6. #6
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: How to Clear Listbox Form Control

    Hmm, no clearing seems to happen although I can step through the code without error.

  7. #7
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: How to Clear Listbox Form Control

    You are awesome tigeravatar and thank you. Your last post did exactly what is needed.

  8. #8
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: How to Clear Listbox Form Control

    You're very welcome

  9. #9
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: How to Clear Listbox Form Control

    A quick question tigeravatar, how does the following know where the Mylist is
    Please Login or Register  to view this content.
    . I looked to see if you named a range but you did not. Are you able to elaborate on this?

  10. #10
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    2003, 2007, 2010, 2013
    Posts
    1,252

    Re: How to Clear Listbox Form Control

    FYI, forms listboxes have a RemoveAllItems method.
    Hope that helps,

    Colin

    RAD Excel Blog

  11. #11
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: How to Clear Listbox Form Control

    @Mordred: There is a named range in the workbook I posted named MyList. It is defined with the following formula:
    =INDIRECT("Chart_Tool!$A$2:$A$"&COUNTA(Chart_Tool!$A:$A))

    @Colin: I was unaware of that, thanks for the tip

  12. #12
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    2003, 2007, 2010, 2013
    Posts
    1,252

    Re: How to Clear Listbox Form Control

    You might be interested to know how I knew that? There's no mystery to these controls if you know where to look.

    Forms controls are hidden members so you can't see them in the object browser unless you right click > show hidden members. You can then locate the Dropdown/ListBox class (which are Forms combobox/listbox) in the Excel object library and see all of the available members. Attached screenshot shows all of this.
    Attached Images Attached Images
    Last edited by Colin Legg; 08-26-2011 at 11:05 AM.

  13. #13
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: How to Clear Listbox Form Control

    You can populate a dropdown wusing .List
    You can clear a dropdown using RemoveAllitems or .List=Array("")

    Please Login or Register  to view this content.
    Last edited by snb; 08-25-2011 at 12:03 PM.



+ 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.6.0 RC 1