+ Reply to Thread
Results 1 to 20 of 20

Populate Userform List box with table data on another sheet

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-13-2014
    Location
    Three Rivers
    MS-Off Ver
    365
    Posts
    129

    Populate Userform List box with table data on another sheet

    Hi,

    I hope someone could help with VBA code to populate a list box on a user form that gets the it's data from another sheet (DB) with all the data in a table. sheet attached

    I need two things to happen with this user form:

    1. When the All Staff button (Grey button) is pressed on the user form then the list box (Resultbox) must get populated with staff detail of the following columns: Name, Last Name, Branch, Cell Number for ALL the staff listed on sheet (DB).

    2. When the Current Staff button (Blue button) is pressed on the user form then the list box (Resultbox) must get populated with staff detail of the following columns: Name, Last Name, Branch, Cell Number for only the staff that are currently employed and this is determined by column N which says if they are employed or not.

    Thanks
    Attached Files Attached Files

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,964

    Re: Populate Userform List box with table data on another sheet

    This for part 1...No time at moment for part 2
    Private Sub AllStaff_Click()
    Dim Arr
    With Sheets("DB").ListObjects(1).Range
        Arr = Application.Index(.Value, Application.Evaluate("row(1:" & .Rows.Count & ")"), Array(2, 3, 5, 9))
    End With
    With Me.ResultBox
        .ColumnCount = 4
        .ColumnWidths = "120;80;80;120"
        .List = Arr
    End With
    End Sub
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  3. #3
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,713

    Re: Populate Userform List box with table data on another sheet

    Try the below codes ...

    Private Sub AllStaff_Click()
    'This code will add all staff details to userform ResultBox
    
    Dim a
    a = Sheets("DB").Range("B2").CurrentRegion
    ResultBox.List = Application.Index(a, Evaluate("row(2:" & UBound(a) & ")"), Array(2, 3, 5, 9))
    
    End Sub
    
    Private Sub CurrentStaff_Click()
    'This code will only show current employed staff details on userform ResultBox by looking at column N ("employed")
    
    Dim a, b
    a = Sheets("DB").Range("B2").CurrentRegion
    
    ReDim b(1 To UBound(a), 1 To 4)
    For X = 1 To UBound(a)
      If X = 2 Or LCase(a(X, 13)) = "yes" Then
        i = i + 1
        b(i, 1) = a(X, 2): b(i, 2) = a(X, 3): b(i, 3) = a(X, 5): b(i, 4) = a(X, 9)
      End If
    Next
    
    ResultBox.List = b
    
    End Sub
    If I was able to help, you can thank me by clicking the * Add Reputation under my user name

  4. #4
    Forum Contributor
    Join Date
    07-13-2014
    Location
    Three Rivers
    MS-Off Ver
    365
    Posts
    129

    Re: Populate Userform List box with table data on another sheet

    Wow this is great!

    If not too much to ask could you please help with code to use the search function when wanting to search employee data listed in columns: Name, Last Name, Branch on "DB" sheet but then to be displayed in the Resultbox as per array in previous code provided.

  5. #5
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,713

    Re: Populate Userform List box with table data on another sheet

    Quote Originally Posted by Rudidw View Post
    .. search employee data listed in columns: Name, Last Name, Branch on "DB"
    Private Sub cmdLookup_Click()
    
    Dim a, b, Txt$
    Txt = txtLookup.Value
    a = Sheets("DB").Range("B2").CurrentRegion
    
    ReDim b(1 To UBound(a), 1 To 4)
    For X = 1 To UBound(a)
      If X = 2 Or InStr(1, a(X, 2), Txt, 1) > 0 Or InStr(1, a(X, 3), Txt, 1) > 0 Or InStr(1, a(X, 5), Txt, 1) > 0 Then
        i = i + 1
        b(i, 1) = a(X, 2): b(i, 2) = a(X, 3): b(i, 3) = a(X, 5): b(i, 4) = a(X, 9)
      End If
    Next
    
    ResultBox.List = b
    
    End Sub

  6. #6
    Forum Contributor
    Join Date
    07-13-2014
    Location
    Three Rivers
    MS-Off Ver
    365
    Posts
    129

    Re: Populate Userform List box with table data on another sheet

    Quote Originally Posted by nankw83 View Post
    Private Sub cmdLookup_Click()
    
    Dim a, b, Txt$
    Txt = txtLookup.Value
    a = Sheets("DB").Range("B2").CurrentRegion
    
    ReDim b(1 To UBound(a), 1 To 4)
    For X = 1 To UBound(a)
      If X = 2 Or InStr(1, a(X, 2), Txt, 1) > 0 Or InStr(1, a(X, 3), Txt, 1) > 0 Or InStr(1, a(X, 5), Txt, 1) > 0 Then
        i = i + 1
        b(i, 1) = a(X, 2): b(i, 2) = a(X, 3): b(i, 3) = a(X, 5): b(i, 4) = a(X, 9)
      End If
    Next
    
    ResultBox.List = b
    
    End Sub

    Thank you the search function works great works great!

  7. #7
    Forum Contributor
    Join Date
    07-13-2014
    Location
    Three Rivers
    MS-Off Ver
    365
    Posts
    129

    Re: Populate Userform List box with table data on another sheet

    EDIT: I have another request regarding this userform...

    I want to add a text box/ list box (please advise which one to use) called ListBox2 that when I single click on a employee in the Resultbox would then automatically populate this other ListBox2 with other different employee info that was not in the ResultBox but this info would be displayed in row format and not columns as before.

    The info displayed in this Listbox2 would be Name, Last Name, ID Number, Tax Number, Address, Email address and Start Date
    Last edited by Rudidw; 06-17-2020 at 10:22 AM.

  8. #8
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,964

    Re: Populate Userform List box with table data on another sheet

    Part 2...
    Private Sub CurrentStaff_Click()
    Dim Arr, Data
    With Sheets("DB").ListObjects(1).Range
        Data = Filter(Evaluate("transpose(if(" & .Columns(13).Address & "=""Yes"",row(1:" & .Rows.Count & ")))"), False, 0)
        If UBound(Data) > -1 Then Arr = Application.Index(.Value, Application.Transpose(Data), Array(2, 3, 5, 9))
    End With
    With Me.ResultBox
        .ColumnCount = 4
        .ColumnWidths = "120;80;80;120"
        .List = Arr
    End With
    End Sub

  9. #9
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,411

    Re: Populate Userform List box with table data on another sheet

    for the OP reduce your DB table to just the used area otherwise @sintek solution adds 2,800 lines of extra 'nothingness' to the listbox.

    ignore above, i was referring to first posting.
    Last edited by torachan; 06-17-2020 at 09:23 AM.

  10. #10
    Forum Contributor
    Join Date
    07-13-2014
    Location
    Three Rivers
    MS-Off Ver
    365
    Posts
    129

    Re: Populate Userform List box with table data on another sheet

    Quote Originally Posted by torachan View Post
    for the OP reduce your DB table to just the used area otherwise @sintek solution adds 2,800 lines of extra 'nothingness' to the listbox.

    ignore above, i was referring to first posting.

    Thanks yes that makes sense!

  11. #11
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,964

    Re: Populate Userform List box with table data on another sheet

    @sintek solution adds 2,800 lines of extra 'nothingness' to the listbox.
    Noticed that too

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Populate Userform List box with table data on another sheet

    Rudidw,

    The code should be like this, otherwise you will get so many balnk rows and unexpected result in certain condition.
    Private Sub AllStaff_Click()
        Dim LR
        With Sheets("db").ListObjects(1).DataBodyRange
            LR = .Parent.Evaluate("max(if(" & .Columns(1).Address & "<>"""",row(" & .Columns(1).Address & ")))") - .Row + 1
            Me.ResultBox.List = Application.Index(.Value, Evaluate("row(1:" & LR & ")"), Array(2, 3, 5, 9))
        End With
    End Sub
    
    Private Sub CurrentStaff_Click()
        Dim x
        Me.ResultBox.Clear
        With Sheets("db").ListObjects(1).DataBodyRange
            x = Filter(.Parent.Evaluate("transpose(if(" & .Columns(13).Address & "=""yes"",row(1:" & .Rows.Count & ")))"), False, 0)
            If UBound(x) > -1 Then
                If UBound(x) = 0 Then
                    Me.ResultBox.Column = Application.Index(.Value, Application.Transpose(x), [{2,3,5,9}])
                Else
                    Me.ResultBox.List = Application.Index(.Value, Application.Transpose(x), [{2,3,5,9}])
                End If
            End If
        End With
    End Sub

  13. #13
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Populate Userform List box with table data on another sheet

    I would use Label (Label1 to Label9)
    Private Sub AllStaff_Click()
        Dim LR
        With Sheets("db").ListObjects(1).DataBodyRange
            LR = .Parent.Evaluate("max(if(" & .Columns(1).Address & "<>"""",row(" & .Columns(1).Address & ")))") - .Row + 1
            Me.ResultBox.List = Application.Index(.Value, Evaluate("row(1:" & LR & ")"), Array(2, 3, 4, 5, 6, 7, 9, 10, 11))
        End With
        With Me.ResultBox
            .ColumnCount = 9
            .ColumnWidths = "100;100;0;100;0;0;100;0;0"
        End With
    End Sub
    
    Private Sub CurrentStaff_Click()
        Dim x
        Me.ResultBox.Clear
        With Sheets("db").ListObjects(1).DataBodyRange
            x = Filter(.Parent.Evaluate("transpose(if(" & .Columns(13).Address & "=""yes"",row(1:" & .Rows.Count & ")))"), False, 0)
            If UBound(x) > -1 Then
                If UBound(x) = 0 Then
                    Me.ResultBox.Column = Application.Index(.Value, Application.Transpose(x), Array(2, 3, 4, 5, 6, 7, 9, 10, 11))
                Else
                    Me.ResultBox.List = Application.Index(.Value, Application.Transpose(x), Array(2, 3, 4, 5, 6, 7, 9, 10, 11))
                End If
            End If
        End With
    End Sub
    
    Private Sub cmdLookup_Click()
        Dim LR, a, i As Long, ii As Long, iii As Long
        With Sheets("db").ListObjects(1).DataBodyRange
            LR = .Parent.Evaluate("max(if(" & .Columns(1).Address & "<>"""",row(" & .Columns(1).Address & ")))") - .Row + 1
            a = Application.Index(.Value, Evaluate("row(1:" & LR & ")"), Array(2, 3, 4, 5, 6, 7, 9, 10, 11))
        End With
        With Me.ResultBox
            .Clear
            For i = 1 To UBound(a, 1)
                For ii = 1 To UBound(a, 2)
                    If UCase$(a(i, ii)) Like "*" & UCase$(Me.txtLookup) & "*" Then
                        .AddItem
                        For iii = 1 To UBound(a, 2)
                            .List(.ListCount - 1, iii - 1) = a(i, iii)
                        Next
                        Exit For
                    End If
                Next
            Next
        End With
    End Sub
    
    Private Sub ResultBox_Click()
        Dim i As Long
        With Me.ResultBox
            For i = 1 To 9
                Me("label" & i).Caption = .List(.ListIndex, i - 1)
            Next
        End With
    End Sub

  14. #14
    Forum Contributor
    Join Date
    07-13-2014
    Location
    Three Rivers
    MS-Off Ver
    365
    Posts
    129

    Re: Populate Userform List box with table data on another sheet

    Private Sub ResultBox_Click()
    Dim i As Long
    With Me.ResultBox
    For i = 1 To 9
    Me("label" & i).Caption = .List(.ListIndex, i - 1)
    Next
    End With
    End Sub[/code][/QUOTE]

    Sorry jindon but total noob here so I have no idea what Label you would be talking about

    I have attached my workbook again with all your code.

    I have added a text box called ListBox2 (bottom right box on user form) which I would like to automatically populate with different info when I select an employee in the Resultbox, but this info would be displayed in row format and not columns as before.

    The info to be displayed in Listbox2 would be Name, Last Name, ID Number, Tax Number, Address, Email address and Start Date

    Thanks for the help so far!
    Attached Files Attached Files

  15. #15
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Populate Userform List box with table data on another sheet

    1) You only use part of my codes. If you are to use mine with other codes regarding this issue, I'm not going to do it, because they are all linked.

    2) I don't understand why/how you want the other list box(ListBox2).

  16. #16
    Forum Contributor
    Join Date
    07-13-2014
    Location
    Three Rivers
    MS-Off Ver
    365
    Posts
    129

    Re: Populate Userform List box with table data on another sheet

    Quote Originally Posted by jindon View Post
    1) You only use part of my codes. If you are to use mine with other codes regarding this issue, I'm not going to do it, because they are all linked.

    2) I don't understand why/how you want the other list box(ListBox2).
    Apologies I see I forgot to save the sheet with all your code in before uploading it here. I have uploaded it again.

    The reason for the other box (ListBox2) is it would show specific details of the employee that was clicked in Resultbox.

    So if you look at the user form, there are 3 boxes:
    1. The big box (Resultbox) - display all staff or employed staff when those buttons are pressed and a search can also be done (this code you have provided and working great...thank you!)
    2. Box bottom right (ListBox2) - will display specific info (Name, Last Name, ID Number, Tax Number, Address, Email address and Start Date) of the employee that is clicked in ResultBox.
    3. Box above ListBox2 (Image1) - will display a photo of the employee that was clicked in ResultBox. this image will be in a folder on computer and named the same number as employee number in table on DB sheet.

    So basically my goal with this userform is - you look for an employee in ResultBox, you click on that employee in ResultBox and then the details specific for that employee (see point 2. above) are displayed in ListBox2 and at the same time a photo of the employee will also be displayed (Image1)...hope this helps to understand this user form better.
    Attached Files Attached Files

  17. #17
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Populate Userform List box with table data on another sheet

    Change to
    Private Sub AllStaff_Click()
        Dim LR
        With Sheets("db").ListObjects(1).DataBodyRange
            LR = .Parent.Evaluate("max(if(" & .Columns(1).Address & "<>"""",row(" & .Columns(1).Address & ")))") - .Row + 1
            Me.ResultBox.List = Application.Index(.Value, Evaluate("row(1:" & LR & ")"), Array(2, 3, 4, 5, 6, 7, 9, 10, 11))
        End With
        With Me.ResultBox
            .ColumnCount = 9
            .ColumnWidths = "100;100;0;100;0;0;100;0;0"
        End With
    End Sub
    
    Private Sub CurrentStaff_Click()
        Dim x
        Me.ResultBox.Clear
        With Sheets("db").ListObjects(1).DataBodyRange
            x = Filter(.Parent.Evaluate("transpose(if(" & .Columns(13).Address & "=""yes"",row(1:" & .Rows.Count & ")))"), False, 0)
            If UBound(x) > -1 Then
                If UBound(x) = 0 Then
                    Me.ResultBox.Column = Application.Index(.Value, Application.Transpose(x), Array(2, 3, 4, 5, 6, 7, 9, 10, 11))
                Else
                    Me.ResultBox.List = Application.Index(.Value, Application.Transpose(x), Array(2, 3, 4, 5, 6, 7, 9, 10, 11))
                End If
            End If
        End With
    End Sub
    
    Private Sub cmdLookup_Click()
        Dim LR, a, i As Long, ii As Long, iii As Long
        With Sheets("db").ListObjects(1).DataBodyRange
            LR = .Parent.Evaluate("max(if(" & .Columns(1).Address & "<>"""",row(" & .Columns(1).Address & ")))") - .Row + 1
            a = Application.Index(.Value, Evaluate("row(1:" & LR & ")"), Array(2, 3, 4, 5, 6, 7, 9, 10, 11))
        End With
        With Me.ResultBox
            .Clear
            For i = 1 To UBound(a, 1)
                For ii = 1 To UBound(a, 2)
                    If UCase$(a(i, ii)) Like "*" & UCase$(Me.txtLookup) & "*" Then
                        .AddItem
                        For iii = 1 To UBound(a, 2)
                            .List(.ListCount - 1, iii - 1) = a(i, iii)
                        Next
                        Exit For
                    End If
                Next
            Next
        End With
    End Sub
    
    Private Sub ResultBox_Click()
        Dim i As Long, myCols
        Me.ListBox2.Clear
        myCols = Array(0, 1, 4, 5, 7, 8)
        Me.ListBox2.ColumnCount = UBound(myCols) + 1
        With Me.ResultBox
            Me.ListBox2.AddItem
            For i = 0 To UBound(myCols)
                Me.ListBox2.List(0, i) = .List(.ListIndex, myCols(i))
            Next
        End With
    End Sub

  18. #18
    Forum Contributor
    Join Date
    07-13-2014
    Location
    Three Rivers
    MS-Off Ver
    365
    Posts
    129

    Re: Populate Userform List box with table data on another sheet

    Thanks jindon

    ListBox2 should be listed in Row format and not Column format as your code works now as I mentioned in a previous post, therefore...

    Name
    Last Name
    Id Number
    Tax Number
    Address
    Email Address
    Start Date

    Thanks

  19. #19
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Populate Userform List box with table data on another sheet

    Ahh.
    Replace "ResultBox_Click" with
    Private Sub ResultBox_Click()
        Dim i As Long, myCols
        Me.ListBox2.Clear
        myCols = Array(0, 1, 4, 5, 7, 8)
        Me.ListBox2.ColumnCount = UBound(myCols) + 1
        With Me.ResultBox
            For i = 0 To UBound(myCols)
                Me.ListBox2.AddItem .List(.ListIndex, myCols(i))
            Next
        End With
    End Sub

  20. #20
    Forum Contributor
    Join Date
    07-13-2014
    Location
    Three Rivers
    MS-Off Ver
    365
    Posts
    129

    Re: Populate Userform List box with table data on another sheet

    Thank you! apologies for only replying now

    I have tested it and all works well.

    Thanks for the help from everyone that posted their solutions.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Populate and configure List box (Userform) from a dynamic range of data.
    By thebeastslayer in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-08-2020, 05:53 AM
  2. [SOLVED] common userform for sheet 1 and sheet 2 but need data to populate from a different row
    By dav2020 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-20-2019, 08:33 PM
  3. [SOLVED] populate userform with data from pivot table
    By Mathijs3 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-09-2016, 10:30 AM
  4. [SOLVED] Populate userform text boxes with dater from another sheet, dependant on list box selectio
    By MallorcaPete in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-17-2015, 07:01 AM
  5. Populate Excel table with userform data
    By waspandbee in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-21-2010, 09:34 PM
  6. Populate a comboxbox and Listbox in a excel userform with an sql table data
    By abhay_547 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-12-2010, 04:58 PM
  7. Populate multi-column list in sheet not in UserForm
    By afzal001 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-12-2008, 12:26 PM

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