how could autofit columns in listbox without specify values for each column

tubrak

Board Regular
Joined
May 30, 2021
Messages
218
Office Version
  1. 2019
Platform
  1. Windows
Hi Guys

I need procedure to fit columns in listbox without specify values . should expand and contracts and control margins for width based on size of data for each column.

so when run the userform or search for data should autofill columns in listbox
I look forward to add some procedures to do that
VBA Code:
Private Sub UserForm_Initialize()
  Dim LastRow As Long
  Set sh = ActiveSheet
  a = sh.Range("A1:F" & sh.Range("A" & Rows.Count).End(xlUp)).Value
  With MY_List
    .ColumnCount = 7
    .ColumnWidths = "20;50;100;100;70;70;70"
    Call CommandButton4_Click
  End With
End Sub
thanks
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I'd say good luck with that idea. Each time you load the listbox the maximum character count over all the list items would have to be determined for each column. Then a width calculation for each count. If you don't use a fixed width font, then still probably would not work because characters like W are far wider than characters such as i, so how to allow for that. Far too much effort for 0 gain. If you're cramped for space then perhaps allow users to see the data in a larger format if they choose a list item that they can't see all of the column data. An example of that could be a userform.
 
Upvote 0
Try this:

VBA Code:
Private Sub UserForm_Initialize()
  Dim i As Long
  Dim sWidths As String
  
  Set sh = ActiveSheet
  sh.Columns("A:F").EntireColumn.AutoFit
  a = sh.Range("A1:F" & sh.Range("A" & Rows.Count).End(xlUp).Row).Value
  
  For i = 1 To UBound(a, 2)
    sWidths = sWidths & Int(sh.Cells(1, i).Width) + 10 & ";"
  Next
  
  With MY_List
    .ColumnCount = UBound(a, 2)
    .ColumnWidths = sWidths
    Call CommandButton4_Click
  End With
End Sub
 
Upvote 0
thanks but shows a problem.
based on previous thread should add column BALANCE when write name or dates for text boxes , but column balance will not add it after replace your updating .
 
Upvote 0
but column balance will not add it after replace your updating
Try this:

VBA Code:
Private Sub UserForm_Initialize()
  Dim i As Long
  Dim sWidths As String
  
  Set sh = ActiveSheet
  sh.Columns("A:G").EntireColumn.AutoFit
  a = sh.Range("A1:G" & sh.Range("A" & Rows.Count).End(xlUp).Row).Value
  
  For i = 1 To UBound(a, 2)
    sWidths = sWidths & Int(sh.Cells(1, i).Width) + 10 & ";"
  Next
  
  With MY_List
    .ColumnCount = UBound(a, 2)
    .ColumnWidths = sWidths
    Call CommandButton4_Click
  End With
End Sub
 
Upvote 0
thanks again
I want share with you the picture
seem the last column could be far in space with comparison the other columns but not much .
I don't want to make hard the matter for you , just I ask if could make it the same space as the others columns.
1.JPG
 
Upvote 0
Try:

VBA Code:
Private Sub UserForm_Initialize()
  Dim i As Long
  Dim sWidths As String
  
  Set sh = ActiveSheet
  sh.Columns("A:F").EntireColumn.AutoFit
  a = sh.Range("A1:F" & sh.Range("A" & Rows.Count).End(xlUp).Row).Value
  
  For i = 1 To UBound(a, 2)
    sWidths = sWidths & Int(sh.Cells(1, i).Width) + 10 & ";"
    If i = 6 Then
      sWidths = sWidths & Int(sh.Cells(1, i).Width) + 10 & ";"
    End If
  Next
  
  With MY_List
    .ColumnCount = UBound(a, 2) + 1
    .ColumnWidths = sWidths
    Call CommandButton4_Click
  End With
End Sub
 
Upvote 0
Another change, with the following columns 5, 6 and 7 (debit, credit, balance) have the same size.

VBA Code:
Private Sub UserForm_Initialize()
  Dim i As Long, nmax As Long
  Dim sWidths As String
  
  Set sh = ActiveSheet
  sh.Columns("A:F").EntireColumn.AutoFit
  a = sh.Range("A1:F" & sh.Range("A" & Rows.Count).End(xlUp).Row).Value
  
  For i = 1 To 4
    sWidths = sWidths & Int(sh.Cells(1, i).Width) + 10 & ";"
  Next
  nmax = WorksheetFunction.Max(Int(sh.Cells(1, "E").Width), Int(sh.Cells(1, "F").Width))
  sWidths = sWidths & nmax + 10 & ";" & nmax + 10 & ";" & nmax + 10
  
  With MY_List
    .ColumnCount = 7
    .ColumnWidths = sWidths
    Call CommandButton4_Click
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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