Column width of ListObject

rdilipk

New Member
Joined
Nov 17, 2011
Messages
36
What is the best way to adjust the width of each column in a ListObject to the maximum length of its content? Currently the column width seems to correctly fit the name of the column but the contents just spill outside.

I see a ListObject.QueryTable.AdjustColumnWidth but that is supposedly true by default.

Yet another way is Range.Column.Autofit but I can't believe I will have to do this for every column in my ListObject.

Clearly I am missing something here. Can someone help?
 

Some videos you may like

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

rdilipk

New Member
Joined
Nov 17, 2011
Messages
36
OK. I just noticed that I can do:

ListObject.Range.Autofit

But my issue with that is the range of my ListObject expands dynamically as I batch-insert rows into it. Would that matter?
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,216
Office Version
  1. 365
Platform
  1. Windows
Why not autofit the listobject's range?
Code:
Dim lst As ListObject

    Set lst = ActiveSheet.ListObjects(1)
    
    lst.Range.Columns.AutoFit
 

rdilipk

New Member
Joined
Nov 17, 2011
Messages
36
Why not autofit the listobject's range?
Code:
Dim lst As ListObject

    Set lst = ActiveSheet.ListObjects(1)
    
    lst.Range.Columns.AutoFit

That works great! Thanks. I missed the Columns property of Range.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,326
Messages
5,595,517
Members
413,995
Latest member
waealu

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
Top