Excel ComboBox - Autosize Dropdown Only

2019-09-10 22:42发布

Is it possible to have just the drop down menu of a ComboBox in a UserForm autofit to the text size, without changing the actual size of the ComboBox?

I've found some answers on how to autofit the actual ComboBox based on the values within, but that makes the size bigger than I actually want (link here).

The following image somewhat represents what I'm trying to accomplish:

enter image description here

Does anyone know if this is even possible?

1条回答
唯我独甜
2楼-- · 2019-09-10 23:10

Some of the columns seem a little wide, but over all I think the code does a pretty good job of configuring the drop down.

enter image description here

Private Sub ConfigureComboBox()
    Dim arrData, arrWidths
    Dim x As Long, y As Long, ListWidth As Double
    arrData = ComboBox1.List
    ReDim arrWidths(UBound(arrData, 2))

    For x = 0 To UBound(arrData, 1)
        For y = 0 To UBound(arrData, 2)

            If Len(arrData(x, y)) > arrWidths(y) Then arrWidths(y) = Len(arrData(x, y))

        Next
    Next

    For y = 0 To UBound(arrWidths)

        arrWidths(y) = arrWidths(y) * ComboBox1.Font.Size
        ListWidth = ListWidth + arrWidths(y)
    Next

    With ComboBox1
        .ColumnCount = UBound(arrWidths) + 1
        .ColumnWidths = Join(arrWidths, ";")
        .ListWidth = ListWidth
    End With

End Sub

Sample data from Excel Sample Data

查看更多
登录 后发表回答