+ Reply to Thread
Results 1 to 2 of 2

Dynamically Display RowSource Data in ListBox

Hybrid View

  1. #1
    Registered User
    Join Date
    05-09-2011
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    38

    Dynamically Display RowSource Data in ListBox

    In my workbook, Sheet1 contains all of the data I want to quantify. Column A contains a list of client names, and Column B contains a numerical value. There are additional columns from C to L with additional data.

    Column A can contain repeat client names on any given row, not necessarily grouped together (i.e. John Doe's name could appear in cell A2, A11, A25, etc). I'm creating a userform that contains a ComboBox (ComboBbox1), a ListBox (ListBox1), and a CommandButton (CommandButton1).

    The ComboBox is already working and alphabetically displays the unique values from Column A. Here's what's I need help with.... After the user selects a client name from the ComboBox and clicks the command button, I would like the ListBox to dynamically display only the records that match the ComboBox1.value. Also, if possible, I would also like these records to be sorted (ascending) by their numerical value contained in Column B.

    This is what my simple code looks like so far. The below codes currently pulls ALL records in my set (from A2:K100). I am not sure how to dynamcially change the RowSource to present the data in this ideal fashion.

    Private Sub CommandButton1_Click()
        With ListBox1
            .ColumnCount = 11
            .ColumnWidths = "100;70;110;100;50;70;100;80;100;100;110"
            .ColumnHeads = True
            .RowSource = Range("A2:K100").Address
        End With
    End Sub
    I want the ListBox to display the all columns, A through K. But only the records that meet the specific criteria.

    Example:

    Client Name Numeric Value Extra Data 1 Extra Data 2 etc,...
    John Doe 13 ... ... ...
    Bobby Jo 45 ... ... ...
    John Doe 2 ... ... ...
    Carter Day 6 ... ... ...
    John Doe 40 ... ... ...

    UserForm ListBox result would be:

    Client Name Numeric Value Extra Data 1 Extra Data 2 etc,...
    John Doe 2 ... ... ...
    John Doe 13 ... ... ...
    John Doe 40 ... ... ...

  2. #2
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Dynamically Display RowSource Data in ListBox

    Try this in your Userform Module:-
    Userfom1 Contains:- ListBox1 and Combobox1
    Option Explicit
    Private Sub UserForm_Initialize()
    Dim Rng As Range, Dn As Range, n As Long
    Dim Dic As Object
    Set Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
    Set Dic = CreateObject("scripting.dictionary")
    Dic.CompareMode = vbTextCompare
    For Each Dn In Rng
        Dic(Dn.Value) = Empty
    Next Dn
    With Me.ComboBox1
        .List = Application.Transpose(Dic.keys)
        .ListIndex = 0
    End With
    End Sub
    
    Private Sub ComboBox1_Change()
    Dim Rng As Range
    Dim Dn As Range
    Dim Ac As Integer
    Dim c As Long
    Dim J, I
    Dim temp(1 To 11)
    Dim n As Integer
    Set Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
    ReDim ray(1 To 11, 1 To Rng.Count)
    For Each Dn In Rng
        If Dn = ComboBox1 Or Dn.Row = 1 Then
            c = c + 1
            For Ac = 1 To 11
                ray(Ac, c) = Dn(, Ac)
            Next Ac
       End If
    Next Dn
    ReDim Preserve ray(1 To 11, 1 To c)
    For I = 2 To UBound(ray, 2)
        For J = I To UBound(ray, 2)
            If ray(2, J) < ray(2, I) Then
              For n = 1 To 11
                temp(n) = ray(n, I)
                ray(n, I) = ray(n, J)
                ray(n, J) = temp(n)
            Next n
            End If
        Next J
    Next I
        
    With ListBox1
            .ColumnCount = 11
            .ColumnWidths = "100;70;110;100;50;70;100;80;100;100;110"
           .List = Application.Transpose(ray)
        End With
    End Sub
    Regards Mick

+ 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. [SOLVED] ListBox RowSource
    By CobraLAD in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-10-2013, 02:08 AM
  2. display content in excel sheet using listbox rowsource vba
    By tkfei in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-09-2011, 05:45 AM
  3. ???Help??? Userform.Listbox.rowsource = ???
    By Steve Sparti in forum Excel General
    Replies: 0
    Last Post: 03-01-2006, 05:50 PM
  4. [SOLVED] Listbox - Rowsource
    By Paul W Smith in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-24-2006, 05:30 PM
  5. [SOLVED] RowSource in ListBox
    By Noah in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-17-2005, 10:10 AM

Tags for this Thread

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