How to Populate a Combobox

前端 未结 3 520
忘掉有多难
忘掉有多难 2021-01-13 01:26

I\'m quite new to VBA and I\'ve been struggling with populating a combobox. I\'m trying to fill a combobox with the contents of the first column in a spreadsheet so I can de

3条回答
  •  时光说笑
    2021-01-13 02:19

    Here is a super simple example of creating and filling a Forms style combo-box:

    Sub FormsStyleComboBox()
        ActiveSheet.DropDowns.Add(411, 14.25, 124.5, 188.25).Select
        N = Cells(Rows.Count, "A").End(xlUp).Row
        strng = Range("A1:A" & N).Address
        Selection.ListFillRange = strng
    End Sub
    

    For example:

    enter image description here

    EDIT#1

    I created a UserForm called Demo containing a combo-box called MyBox

    enter image description here

    In a Standard Module I put:

    Sub DisplayIt()
        Demo.Show
    End Sub
    

    and in the UserForm code area:

    Private Sub UserForm_Initialize()
        Dim N As Long, i As Long
        With Sheets("Sheet1")
            N = .Cells(Rows.Count, 1).End(xlUp).Row
        End With
    
        With MyBox
            .Clear
            For i = 1 To N
                .AddItem Sheets("Sheet1").Cells(i, 1).Value
            Next i
        End With
    End Sub
    

    Running DisplayIt() produces:

    enter image description here

    This is based on this tutorial

提交回复
热议问题