Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA to select all columns in a worksheet and auto adjust all columns width in Excel 2010

Tags:

excel

vba

I have a worksheet with with over 1000 columns. How can I select all columns in that sheet and auto adjust every column width. I tried

Sheets(1).Select
Selection.EntireColumn.AutoFit

But it didn't work. Any ideas?

like image 291
user3720702 Avatar asked Jun 11 '14 03:06

user3720702


People also ask

How do you auto adjust width of all columns in Excel?

Select the column or columns that you want to change. On the Home tab, in the Cells group, click Format. Under Cell Size, click AutoFit Column Width. Note: To quickly autofit all columns on the worksheet, click the Select All button, and then double-click any boundary between two column headings.

How do you AutoFit all cells in Excel VBA?

To AutoFit column width, select one, several or all columns on the sheet, go to the Home tab > Cells group, and click Format > AutoFit Column Width. To AutoFit row height, select the row(s) of interest, go to the Home tab > Cells group, and click Format > AutoFit Row Height.


1 Answers

Try this...

Sheets(1).UsedRange.Columns.AutoFit
like image 105
Mark Balhoff Avatar answered Sep 22 '22 23:09

Mark Balhoff