+ Reply to Thread
Results 1 to 9 of 9

User Form Controls - Connect ComboBox Selection to Listbox

Hybrid View

  1. #1
    Registered User
    Join Date
    03-20-2012
    Location
    LA
    MS-Off Ver
    Excel 2007
    Posts
    32

    User Form Controls - Connect ComboBox Selection to Listbox

    Looking for vba Code that allows the user to select an item from a comboBox that populates a listbox with three columns.

    Private Sub Open_Cmb_Click()
        'This changes the value of the comboBox to year
        Open_Cmb.Value = Format(Open_Cmb.Value, "yyyy")
    End Sub
    
    Private Sub UserForm_Initialize()
        Dim Yrng As Range
        Dim Orng As Range
        Dim Yws As Worksheet
        Dim Ows As Worksheet
        Set Yws = Worksheets("DataLogEcn")
        Set Ows = Worksheets("OpenEcnLog")
        Set Yrng = Yws.Range("Year")
        Set Orng = Ows.Range("OpenYear")
        Me.Year_Cmb.List = Yrng.Value
        Me.Open_Cmb.List = Orng.Value
    
    End Sub
    
    Private Sub Year_Cmb_Click()
        'This changes the value of the comboBox to year
        Year_Cmb.Value = Format(Year_Cmb.Value, "yyyy")
        
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,461

    Re: User Form Controls - Connect ComboBox Selection to Listbox

    You need to provide more information about what 3 bits of data are needed in listbox.

    Private m_blnUpdating As Boolean
    
    Private Sub CmdReportsClose_Click()
    Unload Me
    End Sub
    
    
    Private Sub Open_Cmb_Change()
    
        Dim Orng As Range
        
        If m_blnUpdating = True Then Exit Sub
        
        Set Orng = Worksheets("OpenEcnLog").Range("OpenYear").Cells(Open_Cmb.ListIndex + 1, 1)
        
        m_blnUpdating = True
        With Me.MultiPage1.Pages(0).Open_List
            .AddItem Orng.Offset(0, 1) ' User
            .List(.ListCount - 1, 1) = Orng.Offset(0, 3) 'Signature
            .List(.ListCount - 1, 2) = Orng.Offset(0, 8) 'Project Or H
        End With
        
    End Sub
    
    Private Sub Open_Cmb_Click()
        'This changes the value of the comboBox to year
        Open_Cmb.Value = Format(Open_Cmb.Value, "yyyy")
        
        m_blnUpdating = False
    
    End Sub
    
    Private Sub UserForm_Initialize()
        Dim Yrng As Range
        Dim Orng As Range
        Dim Yws As Worksheet
        Dim Ows As Worksheet
        Set Yws = Worksheets("DataLogEcn")
        Set Ows = Worksheets("OpenEcnLog")
        Set Yrng = Yws.Range("Year")
        Set Orng = Ows.Range("OpenYear")
        Me.Year_Cmb.List = Yrng.Value
        Me.Open_Cmb.List = Orng.Value
    
    End Sub
    
    Private Sub Year_Cmb_Click()
        'This changes the value of the comboBox to year
        Year_Cmb.Value = Format(Year_Cmb.Value, "yyyy")
        
    End Sub
    This code will use the selected row to fill 3 items.

    If you are trying to populate the listbox with all matching records based on year then you will need to loop through the records and test year.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    03-20-2012
    Location
    LA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: User Form Controls - Connect ComboBox Selection to Listbox

    Andy,

    I understand I should have clarified that based on the "Year" selected I would like the list box to display columns "C", "E", and "O".

    thanks

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,461

    Re: User Form Controls - Connect ComboBox Selection to Listbox

    So you just need to alter the Offset positions in the example provided

  5. #5
    Registered User
    Join Date
    03-20-2012
    Location
    LA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: User Form Controls - Connect ComboBox Selection to Listbox

    This how is looks

    ENC Test.png

  6. #6
    Registered User
    Join Date
    03-20-2012
    Location
    LA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: User Form Controls - Connect ComboBox Selection to Listbox

    Andy,

    I notice when I select a year from the drop down, in the list box the header is below the item did I miss something else?

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,461

    Re: User Form Controls - Connect ComboBox Selection to Listbox

    The additem method create a new line within the listbox. The .List() property allows you to fill the specified column of an existing line.
    I think you need to change the 2 additems to List

  8. #8
    Registered User
    Join Date
    03-20-2012
    Location
    LA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: User Form Controls - Connect ComboBox Selection to Listbox

    I notice the add items fill the first column. I'm work on that this afternoon

    thanks

  9. #9
    Registered User
    Join Date
    03-20-2012
    Location
    LA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: User Form Controls - Connect ComboBox Selection to Listbox

    Andy I modified the code which looks correct need a little help connecting the drop down box to the list box. I added an additional worksheet called reports

    Private Sub CmdReportsClose_Click()
    Unload Me
    End Sub
    
    Private Sub Cmp_list_Change()
    
        Dim SourceRange As Excel.Range
        Dim Col1 As String, Col2 As String, Col3 As String
        
        If (Cmp_List.RowSource <> vbNullString) Then
        'Get Range that the List Box is bound to
        Set SourceRange = Range(Cmp_List.RowSource)
        Else
        'Get first row of data
         Set SourceRange = Range("Reports!G2:I2")
         
        Exit Sub
        
    End If
    
        Col1 = Cmp_List.Value
        'Get the value of the second column
        Col2 = SourceRange.Offset(Cmp_List.ListIndex, 1).Resize(1, 1).Value
        'Get the value of the third column
        Col3 = SourceRange.Offset(Cmp_List.ListIndex, 2).Resize(1, 1).Value
        
        'Clean Up
        Set SourceRange = Nothing
        
    
    End Sub
    
    
    
    Private Sub Open_Cmb_Click()
        'This changes the value of the comboBox to year
        Open_Cmb.Value = Format(Open_Cmb.Value, "yyyy")
    
    End Sub
    
    
    Private Sub Open_List_Change()
        Dim SourceRange As Excel.Range
        Dim Col1 As String, Col2 As String, Col3 As String
        
        If (Open_List.RowSource <> vbNullString) Then
        'Get Range that the List Box is bound to
        Set SourceRange = Range(Open_List.RowSource)
        Else
        'Get first row of data
         Set SourceRange = Range("Reports!B2:D2")
         
        Exit Sub
        
    End If
    
        Col1 = Open_List.Value
        'Get the value of the second column
        Col2 = SourceRange.Offset(Open_List.ListIndex, 1).Resize(1, 1).Value
        'Get the value of the third column
        Col3 = SourceRange.Offset(Open_List.ListIndex, 2).Resize(1, 1).Value
        
        'Clean Up
        Set SourceRange = Nothing
        
        End Sub
    
    Private Sub UserForm_Initialize()
        'Set properties of the list box
        With Me.Cmp_List
            .BoundColumn = 1
            .ColumnCount = 3
            .RowSource = "Reports!G2:I2"
            
        With Me.Open_List
            .BoundColumn = 1
            .ColumnCount = 3
            .RowSource = "Reports!B2:D2"
            
        End With
        
        End With
        
    End Sub

+ 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