Archive of Mr Excel Message Board

Back to Errors in Excel archive index
Back to archive home

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

Re: error in auto fit
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.

Re: error in auto fit
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

Re: error in auto fit
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.

Re: error in auto fit
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

This archive is from the original message board at www.MrExcel.com.
All contents © 1998-2004 MrExcel.com.
Visit our
online store to buy searchable CD's with thousands of VBA and Excel answers.
Microsoft Excel is a registered trademark of the Microsoft Corporation.
MrExcel is a registered trademark of Tickling Keys, Inc.