Excel Column autowidth

I am writing an application in VB 6 where I need to resize the column width of an Excel worksheet to that of the maximum size of the content in that column. The property Autofit only makes the width of the columns as wide as that of its caption. But I want it to be done on the basis of the maximum size of its content.

Is there a way to achieve this?

Answers


The autofit function should work fine, just make sure:

  • First insert all data, the call autofit
  • Call autofit on the entire column, not a single cell (e.g. .Cells(1,1).EntireColumn.Autofit()

Hope, this helps...


Your code should look something like this:

Columns("D:D").EntireColumn.AutoFit

This example changes the width of columns A through I on Sheet1 to achieve the best fit:

Worksheets("Sheet1").Columns("A:I").AutoFit

This example changes the width of columns A through E on Sheet1 to achieve the best fit, based only on the contents of cells A1:E1:

Worksheets("Sheet1").Range("A1:E1").Columns.AutoFit

Auto fit will work only for numbers and date.

Please see Excel 2003 XML format - AutoFitWidth not working


  1. Enter your text
  2. place mouse on right top border of column heading
  3. when mouse pointer change
  4. then double click with control key
  5. your column width will be increase according to content....

Need Your Help

Plugin interrupted, invalidated errors for custom keyboard

ios xcode swift keyboard ios-simulator

All of a sudden, a project that used to run well on iOS simulator no longer works. Whenever I try switching to the custom keyboard, I get two errors:

(array) type cast strange behaviour object to array conversion

php json

Earlier I have some block of code and it was working fine, But in some case it is throwing PHP notice: