Autosize Listbox - My Function not fully sizing columns!!

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.:confused:

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
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Forum statistics

Threads
1,223,516
Messages
6,172,777
Members
452,477
Latest member
DigDug2024

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top