VBA: Autofit Columns

August 14th, 2004 | Categories: Cells, Columns & Rows | Tags: , ,

After you manipulate a worksheet with VBA it may be necessary to Autofit your columns to present the nicest end result possible. Here’s how to autofit columns using VBA.

This code autofits columns A and B. The autofit is applied to the active sheet.

Columns("A:B").EntireColumn.Autofit

Get LIVE Help
Custom Excel solutions & live Excel help!
► Go to www.ExcelAutomationHelp.com
► Email Now@ExcelAutomationHelp.com
  1. mWong – PHILIPPINES
    November 4th, 2009 at 01:32
    Reply | Quote | #1

    This works for me Perfectly. Thanks a million.

  2. Martin Reiser
    November 13th, 2010 at 20:43
    Reply | Quote | #2

    A Excel VBA macro in Excel 2007 and 2003 that performs several processes calls another macro that auto fits certain rows, however, the auto fit portion does not produce the intended purpose. However, if I use the Step Into function to execute the macro, the auto fit portion executes properly.

    Do you have any ideaas that might solve this problem?

  3. Martin Reiser
    November 13th, 2010 at 20:47
    Reply | Quote | #3

    A Excel VBA macro in Excel 2007 and 2003 that performs several processes calls another macro that auto fits certain rows height, however, the auto fit portion does not produce the intended purpose. However, if I use the Step Into function to execute the macro, the auto fit portion executes properly.

    Do you have any ideaas that might solve this problem?

  4. Andy
    December 21st, 2010 at 17:29
    Reply | Quote | #4

    Perhaps you need to select the sheet first in the code. I’m guessing when you are stepping through the code you are viewing the sheet you are performing the operation on, but when running the macro this sheet is not automatically selected.

    Try putting this bit of code before your autofit code:

    Sheets(sheetname).select

    Andy :o )