How to auto size listbox column

Irish_Griffin

Board Regular
Joined
Jan 16, 2009
Messages
138
Hello Everyone =-) Hope work / school / life is going well !! :biggrin:

I need to auto size the columns of an excel form listbox.
Thought this would be a matter of finding the longest string and then setting the column width value...... I wish :p

Below, each line of text has 10 characters
xxxxxxxxxx
AAAAAAAAAA
IIIIIIIIII
d8aDNksd9m
bla Blah a

As you can see, the string length is not related to the displayed text width. How can determine the display width value? Perhaps my NooBy-Ness is not aware of a list box option that does column auto sizing for me??

Helps is always appreciated!!
-Griff :rolleyes:
 
try adding the last two lines and let me know
VBA Code:
    Set rng = rng.Resize(UBound(ctr.list) + 1, ctr.ColumnCount)
    rng = v 'ctr.List
    rng.Font.Name = ctr.Font.Size '<-
    rng.Font.Size = ctr.Font.Size + 2 '<-
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
From my experience the best way to resize listbox column widths to the optimum is by using hidden label.
Here is example.
Add label to the userform.
Before code you need to set some properties of this control that may affect font width.

VBA Code:
Private Sub UserForm_Initialize()
   
   Dim vRng As Range, vN1 As Integer, vN2 As Long, vTWidth As Long, vColWidth As String
   Const vSpace = 20

   With Label1
      .Font = ListBox1.Font
      .Font.Bold = ListBox1.Font.Bold
      .Font.Size = ListBox1.Font.Size
      .Font.Italic = ListBox1.Font.Italic
      .AutoSize = True
      .WordWrap = False
      .Visible = False
   End With
   
   With ActiveSheet
      Set vRng = .Range("A2:D10")
      ListBox1.ColumnCount = vRng.Columns.Count
      ListBox1.RowSource = .Name & "!" & vRng.Address
      For vN1 = 1 To vRng.Columns.Count
         For vN2 = 1 To vRng.Rows.Count
            Label1.Caption = vRng.Cells(vN2, vN1).Value
            If Label1.width > vTWidth Then vTWidth = Label1.width
         Next vN2
         vColWidth = vColWidth & "," & vTWidth + vSpace
         vTWidth = 0
      Next
      ListBox1.ColumnWidths = Mid(vColWidth, 2)
   End With
  
End Sub
 
Upvote 0
if you could replace the listbox with a listview here is code to get that done better

Private Declare PtrSafe Function SendMessageLong _
Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) _
As Long

Private Const LVM_FIRST = &H1000
Private Const LVM_SETCOLUMNWIDTH = (LVM_FIRST + 30)

Private Const LVSCW_AUTOSIZE = -1
Private Const LVSCW_AUTOSIZE_USEHEADER = -2


Private Sub UserForm_Activate()
For counter = 1 To ListView1.ColumnHeaders.count
Call SendMessageLong(ListView1.hwnd, LVM_SETCOLUMNWIDTH, counter - 1, LVSCW_AUTOSIZE_USEHEADER)
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,427
Messages
6,124,831
Members
449,190
Latest member
rscraig11

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