Hi All!
I am trying to dynamically assign column width for each column in a listbox. I am calculating the maximum length of a string in a column and assigning maximum length + 5 as the column width. But when the list box pops up after the code is executed, all the column values still remain truncated which clearly shows that column widths are not assigned at all.
I would really appreciate it if you could please tell how I need to modify the code.
Thanks,
PK
____________________________________________________________________________________
[pre]
[/pre]
I am trying to dynamically assign column width for each column in a listbox. I am calculating the maximum length of a string in a column and assigning maximum length + 5 as the column width. But when the list box pops up after the code is executed, all the column values still remain truncated which clearly shows that column widths are not assigned at all.
I would really appreciate it if you could please tell how I need to modify the code.
Thanks,
PK
____________________________________________________________________________________
[pre]
Code:
Sub Mstr_Project()
Dim Column_Count As Integer
Dim Row_Count As Integer
Dim i As Integer 'row counter
Dim j As Integer ' column counter
Dim strLength As Integer
Dim MaximumStringLength As Integer
Table.Label1.Caption = "[dbo].[mstr_Project]"
Column_Count = Worksheets("Description").Range("Mstr_Project").Columns.Count
Row_Count = Worksheets("Description").Range("Mstr_Project").Rows.Count
Table.ListBox1.ColumnCount = Column_Count
Table.ListBox1.List = Worksheets("Description").Range("Mstr_Project").value
For j = 1 To Column_Count 'count of columns
For i = 1 To Row_Count 'count of rows within jth column
strLength = Len(Worksheets("Description").Range("Mstr_Project").Cells(i, j).value)
If strLength > MaximumStringLength Then
MaximumStringLength = strLength
End If
Next i
Table.ListBox1.ColumnWidths = MaximumStringLength + 5
Debug.Print Table.ListBox1.ColumnWidths
Next j
Table.Show
End Sub