MrExcel Publishing
Your One Stop for Excel Tips & Solutions

error in auto fit


Posted by David on July 06, 2001 8:11 AM

Worksheets("Data").Cells(5, x).Select
Worksheets("Data").Cells(5, x).Columns.AutoFit
Worksheets("Data").Cells(5, x).ColumnWidth = Worksheets("Data").Cells(5, x).ColumnWidth + 2

this is in a loop
how do I do this without selecting the columns

in other word I want every column to be auto fit then ad 2 to it without sellecting any columns


Posted by Ben O. on July 06, 2001 8:59 AM

Try this:

With Worksheets("Data").Columns(x)
.AutoFit
.ColumnWidth = .ColumnWidth + 2
End With


-Ben O.

Posted by David on July 06, 2001 12:48 PM

That was great.
Can you do it that easily for every column without a loop

Posted by Ben O. on July 06, 2001 2:19 PM


Yes, just remove the x so it reads Columns(). That will run the With statement for every column.

You could also use:

For x = 1 to Columns.Count
.
.
.
Next x


-Ben O.

Posted by David Rainey on July 09, 2001 12:08 PM

Autofit method of range failed

For x = 1 To Columns.Count
With Worksheets("Data").Columns(x)
.AutoFit
.ColumnWidth = .ColumnWidth + 2
End With
next