+ Reply to Thread
Results 1 to 2 of 2

Populate Combobox, controlled by another Combobox !

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-05-2008
    Location
    Kiruna
    Posts
    163

    Populate Combobox, controlled by another Combobox !

    Hi i need a little help.

    If i have two comboboxes, ComboBox1 and ComboBox2.

    In ComboBox1 i can choose eiter "sheet5" or "sheet6"

    If i choose "sheet5" i want ComboBox2 to populate whit the data in columns A and B in "sheet1"
    and i if i choose "sheet6" i want ComboBox2 to populate whit the data in columns A and B in "sheet2"
    Private Sub ComboBox1_Change()
        If ComboBox1.ListIndex <> -1 Then
            ListBox1.Clear
            With Sheets(ComboBox1.Text)
                ListBox1.List = .Range(.Cells(1, 3), .Cells(.Rows.Count, 1).End(xlUp)).Value
            End With
        End If
    End Sub
    
    
    Private Sub ListBox1_Change()
        With ListBox1
            If .ListIndex <> -1 Then
                CommandButton2.Enabled = True
                TextBox1.Text = .Value
                TextBox2.Text = .Text
                TextBox3.Text = .Text
            Else
                CommandButton2.Enabled = False
                TextBox1.Text = vbNullString
                TextBox2.Text = vbNullString
                TextBox3.Text = vbNullString
            End If
        End With
    End Sub
    
    Private Sub CommandButton2_Click()
        Sheets(ComboBox1.Text).Range("A1").Offset(ListBox1.ListIndex, 0).EntireRow.Delete
        Call ComboBox1_Change
    End Sub
    
    Private Sub UserForm_Initialize()
        For i = 4 To Sheets.Count - 1
            ComboBox1.AddItem Sheets(i + 1).Name
        Next i
        
        With ListBox1
            .ColumnCount = 3
            .ColumnWidths = "60;"
            .BoundColumn = 1
            .TextColumn = 2
        End With
        CommandButton2.Enabled = False
        ComboBox1.ListIndex = 0
    End Sub
    
    Private Sub CommandButton1_Click()
        Dim ws As Worksheet, txtb As Control
        Dim aCell As Range, iRow As Integer
    
        If Len(Trim(TextBox1.Value)) = 0 Then
            MsgBox "Please enter a Account Number"
            TextBox1.SetFocus
            Exit Sub
        End If
        If Len(Trim(TextBox2.Value)) = 0 Then
            MsgBox "Please enter Item Name"
            TextBox2.SetFocus
            Exit Sub
        End If
     
        Set ws = Sheets(ComboBox1.Text)
        
        Set aCell = ws.Columns(1).Find(What:=Trim(TextBox1.Value), LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=True, SearchFormat:=False)
        
        If Not aCell Is Nothing Then
            MsgBox "Account Already Exists"
        Else
            iRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
            If ws.Cells(1, 1) <> "" Then iRow = iRow + 1
             ws.Cells(iRow, 1) = TextBox1.Value
            ws.Cells(iRow, 2) = TextBox2.Value
            ws.Cells(iRow, 3) = TextBox2.Value
        End If
        
        '~~> Clear All textboxes
        For Each txtb In Me.Controls
            If TypeName(txtb) = "TextBox" Then
                txtb.Text = ""
            End If
        Next
        Call ComboBox1_Change
        
        
    End Sub
        
    Private Sub CommandButton3_Click()
        Unload Me
    End Sub

    Best regards
    Petter
    Attached Files Attached Files

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Populate Combobox, controlled by another Combobox !

    Hi Petter120
    In the attached I've added two Dynamic Named Ranges that become the Row Source for ComboBox2. I've also added this code
    Private Sub ComboBox1_Click()
        Select Case Me.ComboBox1.ListIndex
        Case 0
            Me.ComboBox2.RowSource = "Test1"
        Case 1
            Me.ComboBox2.RowSource = "Test2"
        End Select
    End Sub
    Let me know of issues.
    Attached Files Attached Files
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

+ 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