+ Reply to Thread
Results 1 to 4 of 4

Populating Userform Listbox with Dynamic Range

  1. #1
    Registered User
    Join Date
    03-09-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    4

    Populating Userform Listbox with Dynamic Range

    I'm trying to develop a listbox that will change its list depending on another listbox that is selected. The number of items changes, so sometimes it will need to display 50 items and sometimes only 5. I want the code to recognize the last cell in the column with data entered and no more, so that the listbox doesn't have a long scroll bar for no reason.

    Here is the code I have thus far, but it does not work.

    Private Sub ListBox1_Click

    Worksheets("Fed_feedback").Range("$Q$2").ClearContents
    Dim lSelected As Long

    Dim r As Excel.Range
    For lSelected = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(lSelected) = True Then
    Sheet3.Range("Q2") = ListBox1.List(lSelected)
    ListBox1.Selected(lSelected) = False
    End If
    Next
    Call ProfSort

    Dim i As Long, MyUniqueList2 As Variant
    With UserForm1.ListBox2
    .ColumnCount = 1
    .Clear
    MyUniqueList2 = UniqueItemList(Range("DynamicCourseList"), True)
    For i = 1 To UBound(MyUniqueList2)
    .AddItem MyUniqueList2(i)
    Next i
    .ListIndex = 0
    End With
    The part in bold is the problem. 1) I do not want to define the range by a specific number of cells as I have with "DynamicCourseList", I want a code to only define the range with just enough for the filtered data. 2) the code as is doesn't work anyway. I keep getting an error for For i = 1 To Ubound(MyUniqueList2) line.

    Thoughts??

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Populating Userform Listbox with Dynamic Range

    I reckon you need to provide a workbook or at least all the code you are calling in that snippet
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Registered User
    Join Date
    03-09-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Populating Userform Listbox with Dynamic Range

    Quote Originally Posted by JosephP View Post
    I reckon you need to provide a workbook or at least all the code you are calling in that snippet
    Are you referring to the Call ProfSort? That is just a macro to do the advanced filtering in the first listbox. It's unrelated to what I'm trying to do in the second....I think. But here is the module for that

    Sub ProfSort()
    Range("$A$1:$P$2717").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range _
    ("$Q$1:$R$2"), CopyToRange:=Range("$S$1:$AH$2717"), Unique:=False
    End Sub
    I guess to give a better understanding of what I'm trying to do overall: the first listbox is filtering by professor name upon click. Those filtering results include a new course listing that I want to populate the second list box with. I want to input a code that does so, but is dynamic to the amount of items resulting from the first filtering.

    Does that make things a little clearer, or am I still providing the wrong info? (bear with me, I'm a newbie, but I'm trying!)

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Populating Userform Listbox with Dynamic Range

    your code calls UniqueItemList but we don't have the code for that which seems key to part of your question. we also don't know what your ranges refer to or really what your code is meant to be doing-you seem to overwrite Q2 repeatedly with each selected item in listbox 1?

+ 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