lewisgmorris
Board Regular
- Joined
- Oct 23, 2014
- Messages
- 119
I've read online of a way to work out the column widths by applying the longest string of each listbox column to a hidden label and then setting the columnwidths property of the hiddenlabel.width to each column.
Its ALMOST working but it just falls short of fully being to size. I've noticed every time i set the MSforms.Listbox object the "Listbox" part goes to lower case?
Any ideas appreciated?
Its ALMOST working but it just falls short of fully being to size. I've noticed every time i set the MSforms.Listbox object the "Listbox" part goes to lower case?
Any ideas appreciated?
Code:
Function autofitColumnsFromListbox(hiddenlab As MSForms.Label, listbox As MSForms.listbox, usrfm As Object) As String
Dim totalwidth As Double
Dim x As Integer
Dim p As Integer
Dim widthproperty As String
Dim tempstring
Dim arr() As Variant
Dim longestString As String
For x = 0 To listbox.ColumnCount - 1
longestString = ""
For p = 0 To listbox.ListCount - 1
If longestString < listbox.List(p, x) Then longestString = listbox.List(p, x)
Next p
hiddenlab.Caption = longestString
widthproperty = widthproperty & hiddenlab.Width & ","
totalwidth = totalwidth + hiddenlab.Width
Next x
listbox.ColumnWidths = widthproperty
listbox.Width = totalwidth
usrfm.Width = listbox.Width + ((listbox.Width / 100) * 2.5)
End Function