0

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?

1
  • Please review you previous questions and mark as answers where appropriate Commented May 20, 2011 at 15:23

5 Answers 5

3

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...

Sign up to request clarification or add additional context in comments.

Comments

3

Your code should look something like this:

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

Comments

3

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

Comments

2

Auto fit will work only for numbers and date.

Please see Excel 2003 XML format - AutoFitWidth not working

Comments

0
  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....

1 Comment

The user is asking how to programatically increase the width to the largest value, not how a user would manually do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.