Consulting

Results 1 to 4 of 4

Thread: Fill a combobox with data found in a range based on worksheet chosen in another cbx

  1. #1
    VBAX Newbie
    Joined
    Mar 2014
    Posts
    2
    Location

    Fill a combobox with data found in a range based on worksheet chosen in another cbx

    I have a userform with two comboboxes. The first combobox populates with the names of each worksheet in the workbook. How do I get the second combobox to populate with column headings in the worksheet chosen in the first combobox

  2. #2
    VBAX Tutor david000's Avatar
    Joined
    Mar 2007
    Location
    Chicago
    Posts
    276
    Location
    Private Sub ComboBox1_Change()
    Dim iCol As Integer
    Dim cel As Range
    Dim ws As Worksheet
    Set ws = Worksheets(ComboBox1.ListIndex + 1)
    ComboBox2.Clear
    With ws
        iCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
        For Each cel In .Range("a1").Resize(, iCol)
        ComboBox2.AddItem cel.Value
        Next cel
    End With
    
    End Sub
    Private Sub UserForm_Initialize()
    Dim i As Integer
    With ActiveWorkbook
        For i = 1 To Worksheets.Count
        With ComboBox1
            .AddItem Sheets(i).Name
        End With
        Next i
    End With
    End Sub
    Attached Files Attached Files
    "To a man with a hammer everything looks like a nail." - Mark Twain

  3. #3
    VBAX Newbie
    Joined
    Mar 2014
    Posts
    2
    Location

    Additional help needed

    Thank You! That worked perfectly

    However I have another issue. The data entered into a form will be entered onto a the chosen worksheet. Two items entered will always be entered into the same column, one item however needs to be entered into a cell based on the column heading chosen combobox2 (which varies from worksheet to worksheet). How can I manage this? I cannot simply state:

    ActiveCell.Offset(0, 0) = Me.PurchaseDate.Text
    ActiveCell.Offset(0, 1) = Me.Description.Text
    ActiveCell.Offset(0, 2) = Amount.text (Depending on which column header is chosen in checkbox2 the amount may need to be offset additional columns)
    Last edited by lambr; 03-15-2014 at 09:54 AM. Reason: Additional help needed

  4. #4
    try like
    col = activesheet.range("1:1").find(combobox2.text).column
    ActiveCell.Offset(0, col - 1) = Amount.text
    not tested

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •