Can we read the ColumnWidths of a ListBox ?

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,797
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I need to read the width of each column in a multi-column ListBox ... It seems that the ColumnWidths Property is Write Only.
Am I missing something ? Any thoughts ?

Thank you.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Upvote 0
I seem to be able to read it.
VBA Code:
Private Sub CommandButton1_Click()
    MsgBox Me.ListBox1.ColumnWidths
End Sub
 
Upvote 0
Based on Nories' reply, you can iterate to read each column individually using Split
VBA Code:
Private Sub CommandButton1_Click()
    Dim e
    For Each e In Split(Me.ListBox1.ColumnWidths, ";")
        Debug.Print e
    Next e
End Sub
 
Upvote 0
Thanks guys.

MsgBox Me.ListBox1.ColumnWidths Returns an empty string for Sheet ListBoxes as well as for UserForms Listboxes. (I am using Excel 2016 if that makes a difference and I had the Listbox populated using RowSource)
 
Upvote 0
Have you specified the column widths property like that for example: 10 pt;20 pt;30 pt ??
I have tested on Office 365 and it worked .. I have assigned RowSource for the listbox on userform
 
Upvote 0
Have you specified the column widths property like that for example: 10 pt;20 pt;30 pt ??
I have tested on Office 365 and it worked .. I have assigned RowSource for the listbox on userform
Good point YasserKhalil !

I didn't set the column widths prior to reading them ... The code indeed works if the column widths are explicitly set before hand either via code or via the Properties Window in the VBE. So thank you for that.

I wonder if there is a Property or another way to read the individual column widths if they are not set beforehand.

I noticed that, if the column widths are not explicitly set, Excel gives each individual column the default fixed width of (≅ 82.5 pt) but I am not sure that this value will be always the same regardless of the user specifics.
 
Upvote 0
According to the documentation:

To calculate column widths when ColumnWidths is blank or -1, the width of the control is divided equally among all columns of the list. If the sum of the specified column widths exceeds the width of the control, the list is left-aligned within the control and one or more of the rightmost columns are not displayed. Users can scroll the list using the horizontal scroll bar to display the rightmost columns.

The minimum calculated column width is 72 points (1 inch). To produce columns narrower than this, you must specify the width explicitly.
 
Upvote 0
Solution

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,620
Latest member
dsubash

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