Column Width

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Have a data set which results in column widths required to be set at a specific width. Is there a way to perform this task, the data can extend to many more than the example below.

Auto fit does not work since the data in some cells may be larger.

example


Range("A1").ColumnWidth = 14.5
Range("B1").ColumnWidth = 22#
Range("C1").ColumnWidth = 23.5
Range("D1").ColumnWidth = 8.5
Range("E1").ColumnWidth = 28.5
Range("F1").ColumnWidth = 12.5
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
You mean like this

Code:
Columns("A:A").AutoFit
 Columns("B:B").AutoFit
 Columns("C:C").AutoFit
 
Upvote 0
Another option would be
Code:
Sub ColWidth()

   Dim Col As Range
   For Each Col In Range("A1", Cells(1, Columns.Count).End(xlToLeft))
      Col.ColumnWidth = Len(Col)
   Next Col

End Sub
This will set the col width depending on the length of the value in row 1 for each col. If needed you could also use this
Code:
Col.ColumnWidth = Len(Col)+3
to make the cols a little bit wider
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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