+ Reply to Thread
Results 1 to 6 of 6

tearing hair out - Listbox collection

  1. #1
    Forum Contributor
    Join Date
    12-04-2003
    Posts
    360

    tearing hair out - Listbox collection

    I have a userform with 5 listboxes on it.

    It there a neater way of cycling through each listbox control rather than evaluating each one in turn?

    Something like

    for each lb in userform.listboxes
    msgbox lb.name
    next lb

    CIA,
    Matt.

  2. #2
    Forum Contributor
    Join Date
    06-10-2004
    Location
    India
    Posts
    1,066
    There should be a way to check if the control is a listbox type using something like:
    msoControl... I am not able to find right now.

    Using code like:

    For Each lb In UserForm1.Controls
    MsgBox lb.Name
    Next

    lists all the controls in your userform. Using an if statement, you can limit it to all listbox types.

    A simple workaround would be to check for the names of these controls. Suppose you have named them ListBox1, ListBox2... till 5, then you could use:

    For Each lb In UserForm1.Controls
    if left(lb.name,7)="ListBox" then
    MsgBox lb.Name
    end if
    Next


    Mangesh

  3. #3
    Forum Contributor
    Join Date
    06-10-2004
    Location
    India
    Posts
    1,066
    This worked for me nicely:

    For Each lb In UserForm1.Controls
    If LCase(Left(lb.Name, 7)) = "listbox" Then
    MsgBox lb.Name
    End If
    Next

    But the names of the listboxes need to be starting with listbox___

    for example:
    Listbox1
    Listbox2
    Listbox3
    ListboxFour
    etc.

    Mangesh

  4. #4
    Bob Phillips
    Guest

    Re: tearing hair out - Listbox collection

    Dim oCtl As MSForms.Control

    For Each oCtl In Me.Controls
    If TypeName(oCtl) = "ListBox" Then
    MsgBox oCtl.Name
    End If
    Next oCtl


    --
    HTH

    Bob Phillips

    "MattShoreson" <MattShoreson.1qkaie_1118660706.34@excelforum-nospam.com>
    wrote in message
    news:MattShoreson.1qkaie_1118660706.34@excelforum-nospam.com...
    >
    > I have a userform with 5 listboxes on it.
    >
    > It there a neater way of cycling through each listbox control rather
    > than evaluating each one in turn?
    >
    > Something like
    >
    > for each lb in userform.listboxes
    > msgbox lb.name
    > next lb
    >
    > CIA,
    > Matt.
    >
    >
    > --
    > MattShoreson
    > ------------------------------------------------------------------------
    > MattShoreson's Profile:

    http://www.excelforum.com/member.php...fo&userid=3472
    > View this thread: http://www.excelforum.com/showthread...hreadid=378495
    >




  5. #5
    Forum Contributor
    Join Date
    12-04-2003
    Posts
    360
    cheers chaps.

  6. #6
    Forum Contributor
    Join Date
    12-04-2003
    Posts
    360

    Spoke too soon!

    Right the code to cycle thru ctls is cool. Works a treat.
    However as the control is a multiselect combo...

    I need to evaluate the selected items and the control object doesnt have a listbox prop.

    Any ideas on how to possibly assign the object to have listbox props or will I just have to change the object to a variant type?

+ 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