Results 1 to 2 of 2

Populate Combobox, controlled by another Combobox !

Threaded 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

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