Author: Oscar Cronquist Article last updated on April 01, 2019

Excel does not resize columns as you type by default as the image above demonstrates. You can easily resize all columns manually by press with left mouse button oning on the column header next to column A and then double press with left mouse button on with left mouse button on any of the delimiting column lines.

Double-press with left mouse button oning on the column next to column A selects all cells on the worksheet, double-press with left mouse button oning on any of the delimiting column lines resizes all columns containing data so it fits accordingly.

You can also press short-cut keys CTRL + A in order to select all cells on the worksheet. Note that the selected cell must be empty before you press CTRL + A. If not, a cell range containing contiguous adjacent non-empty cells will be selected.

There is a short-cut for auto adjusting column widths as well, simply press Alt + H + O + I. Make sure you have selected the cells you want to to auto adjust before pressing the short-cut keys.

You can, however, create an Event that does this for you automatically. Let me show you how to create self-adjusting columns in Excel with a few lines of VBA code.

Example,

You can also copy and paste cells and the columns resize instantly.

Example,

The Event code displayed below is not placed in a regular code module, this event code is placed in the Workbook module, event code is rund when a specific thing happens.

For example, a cell is selected, a worksheet is activated, a workbook is closed, a cell value has changed etc. All these actions can trigger an Event, which Event is determined by the name of the macro.

VBA code

'The WorkbookSheetChange event is run if any of the cells in the workbook changes
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

'Don't show changes made to the worksheet
Application.ScreenUpdating = False

'Go through each cell that has been changed
For Each Value In Target.Columns

'Auto resize column using the Autofit method
    Worksheets(Sh.Name).Columns(Value.Column).AutoFit

'Continue with next cell that has changed
Next Value

'Show changes to the user
Application.ScreenUpdating = True

End Sub

The Target parameter is a range object containing the cell or cells that have been changed, the Sh parameter contains the worksheet object of the changed cells.

The ScreenUpdating property allows you to perform changes to the workbook without showing the result until all changes have been made, this makes the code run faster and smoother.

Where to copy code?

  1. Press Alt + F11 to open the VBA Editor.
  2. Double-press with left mouse button on ThisWorkbook in Project Explorer
  3. Copy VBA code
  4. Paste to the code module
  5. Exit VBA editor and return to Excel

Final thoughts

The downside with this approach is that you lose the ability to undo any changes you make, there is, however, a solution to that: Generic undo routine for VBA

If you want to use two lines in one cell simply press Alt+Enter where you want the text to be split.

Get the Excel file


Auto-resize-columns.xlsm