ComboBox.ListWidth question

  • Thread starter Thread starter Joepy
  • Start date Start date
J

Joepy

Hello,
I have a form with several comboboxes. However, if the text from the
RowSource is longer than the combobox itself, I'd like the _list_ to
become wider (not the combobox), so that the longest entry in my
RowSource Range fits in.

However, the Property Inspector only lets me use " x pt" values under
ListWidth, but I don't want to provide a fixed width for my boxes.

Does anyone know how this can be done?

Thanks,Joepy
 
Joepy

You could use a sub like this in the drop button click event to resize based
on the list

Private Sub ComboBox1_DropButtonClick()
Dim Rng As Range
Dim cell As Range
Dim MaxLen As Long

Set Rng = Me.Range(Me.ComboBox1.ListFillRange)

For Each cell In Rng.Cells
MaxLen = Application.Max(Len(cell.Text), MaxLen)
Next cell

Me.ComboBox1.ListWidth = Application.Max(MaxLen * 5, 30)
End Sub

This example is from a combobox on a sheet, so if you are using a userform,
you may need to modify it slightly.
 
Back
Top