4

In Excel if you double-click a column on its right "border" you can auto-size the width to be the width of the largest value in that column.

How does one replicate this in VBA?

3
  • 2
    Record a macro and see? (range.entirecolumn.autofit)
    – Alex K.
    Commented Jul 9, 2012 at 13:56
  • Apologies- completely forgot I could do that!!! Commented Jul 9, 2012 at 13:57
  • Post it as an answer and I will accept- I think you were first anyway Commented Jul 9, 2012 at 14:07

2 Answers 2

6

You can use the Macro Recorder to perform the action & see;

<range>.entirecolumn.autofit
1
  • 1
    + 1 for suggesting the Macro Recorder. Commented Jul 9, 2012 at 14:15
3

Call the AutoFitMethod of a Columnobject.

Like so:

ActiveSheet.UsedRange.Columns.AutoFit

or like so:

Columns("A:A").EntireColumn.AutoFit
1
  • Same logic to Auto resize a row's height ;)
    – Francis P
    Commented Jul 9, 2012 at 13:56

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.