Consulting

Results 1 to 8 of 8

Thread: Solved: Adding select rows to a listbox in a form

  1. #1
    VBAX Tutor
    Joined
    Oct 2007
    Posts
    210
    Location

    Solved: Adding select rows to a listbox in a form

    What I want to do is use autofilter to limit the list then only add the filtered rows to the listbox. Is this possible?
    "The amount of stupid people in the world is God's way of punishing the smart people" - protean_being

  2. #2

    Lightbulb

    [vba]

    Sub OnlyFiltered()
    Dim MyCell as Range

    For each MyCell in ActiveSheet.Range("A1:A100")
    If not MyCell.EntireRow.Hidden then

    ''' Do a bunch of stuff

    End If
    Next MyCell


    [/vba]

  3. #3
    VBAX Tutor
    Joined
    Oct 2007
    Posts
    210
    Location
    It's the "Do a bunch of stuff" I need help with. How do I enter the data (multi-columns) into the list?
    "The amount of stupid people in the world is God's way of punishing the smart people" - protean_being

  4. #4
    Create a userform "Userform1" and put a list box on it , "Listbox1"


    [vba]Sub LoadSomeUserFormListBox()
    Dim ListArr(1 To 2, 1 To 2) As String

    ListArr(1, 1) = "Elem 1,1"
    ListArr(1, 2) = "Elem 1,2"
    ListArr(2, 1) = "Elem 2,1"
    ListArr(2, 2) = "Elem 2,2"

    UserForm1.ListBox1.List() = ListArr

    ' Show the UserForm
    UserForm1.Show
    End Sub
    [/vba]

  5. #5
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    This worked for me.
    Private Sub UserForm_Activate()
    Dim onecell As Range
    With Me.ListBox1
        .columnscount = 5
        For Each onecell In ThisWorkbook.Sheets("sheet1").Range("A1:a10").SpecialCells(xlCellTypeVisible)
            .AddItem onecell.Value
            .List(.ListCount - 1, 1) = onecell.Range("b1").Value
            .List(.ListCount - 1, 2) = onecell.Range("c1").Value
            .List(.ListCount - 1, 3) = onecell.Range("d1").Value
            .List(.ListCount - 1, 4) = onecell.Range("e1").Value
        Next onecell
    End With
    End Sub
    (If you have more than 10 columns, this technique will not work.)

    The trick is that ListBox1.List is zero based, so when the .ColumnCount is 5, the highest column index is 4. Hence, the -1 in the .ListCount-1.

  6. #6
    VBAX Tutor
    Joined
    Oct 2007
    Posts
    210
    Location
    This worked good. However I want a header in this list. Is that possible?
    "The amount of stupid people in the world is God's way of punishing the smart people" - protean_being

  7. #7
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Not on my Mac.
    A Windows user would have to speak to that situation.

  8. #8
    VBAX Tutor
    Joined
    Oct 2007
    Posts
    210
    Location
    I just put labels above the list. Thanks for your assistance!!
    "The amount of stupid people in the world is God's way of punishing the smart people" - protean_being

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •