Auto resize columns as you type
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?
- Press Alt + F11 to open the VBA Editor.
- Double-press with left mouse button on ThisWorkbook in Project Explorer
- Copy VBA code
- Paste to the code module
- 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.
Macro category
This article demonstrates how to add or remove a value in a regular drop down list based on a list […]
In this tutorial, I am going to show you how to add values to a drop down list programmatically in […]
This article demonstrates how to place values automatically to a table based on two conditions using a short macro. Cell […]
Excel categories
20 Responses to “Auto resize columns as you type”
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Oscar,
what code would you suggest in order to allow column resizing in a protected worksheet?
whereas some cells are locked.
would still the vba encounter a Run-time error '424' if a line was added to "bypass" the protection?
(un protect, modify,re-protect)
such as:
Sheets(“Sheet1″).Unprotect Password:=” whatever ”
PROCEDURE
Sheets(“Sheet1).Protect Password:=” whatever “
with the risk of having the sheet unprotected if the procedure fails...
or something like
Private Sub Workbook_Open()
Sheets (" sheetname ") .protect _
Password:=" whatever", _
UserInterfaceOnly:=True
End Sub
Thanks.
Oops,
forgot to add,
would this be included in the first code or should it be a separate code?
Cyril,
You can select "Format rows" and "Format columns" when you protect a sheet.
Now you can use the macro in a protected sheet.
Yes... true.
although I locked the sheet to limit the width... I have many users and some tend to be "artistic" and revamp the files.
Thanks for the reminder.
Cyril,
Try this code:
Thanks Oscar!
Could this work for rows? I have a column I need to stay the same size but I need the rows to be re-sized as the user types. Can you help?
Yogi,
Yes!
Get the Excel *.xlsm file
Auto-resize-rows.xlsm
Hi! How can I make it work for selected worksheets rather than whole workbook? I need to have one or two worksheets where the column width is set manually. Many thanks for your help in advance.
Justine,
1. Press with right mouse button on a sheet name
2. Press with left mouse button on "View Code.."
3. Copy / Paste the code below
4. Return to excel (Alt + Q)
Thank you so much, this is very helpful. I have two worksheets where I already have a code for automatic sorting, and when I paste this second code to the editor then try to add new data I get an error message - Compile error: Ambiguous name detected: Worksheet_Change. Any idea what am I doing wrong? I'll be most grateful for your help.
Justine,
It seems like you have two Worksheet_Change subroutines. You can only have one.
This is great. What change needs to be done in order for it to work with dynamic data (like data populated with index match or vlookup or lookup and contat formula you have). I have my version from your replies as below. Instead of change I thought triggering event with Calculate would work, but it did not.
Also we have about 2000 rows loading dynamically and do not want to overload excel. Thanks a lot in advance.
Private Sub Worksheet_Calculate(ByVal Target As Range)
Application.ScreenUpdating = False
Worksheets(Sh.Name).Unprotect Password:="Password"
On Error Resume Next
For Each Value In Target.Columns
Worksheets(Sh.Name).Rows(Value.Column).AutoFit
Next Value
On Error GoTo 0
Worksheets(Sh.Name).Protect Password:="Password"
Application.ScreenUpdating = True
End Sub
I meant "Lookup_concat" user formula that you mentioned in other tutorial.
Why am I getting "variable not defined" error on first line of the code?
Andrew,
Did you put the code in the workbook module?
Oscar,
This works perfectly for resizing the columns, so thanks for that! I just noticed, though, that the sheet no longer allows for an 'Undo'. Ctrl-Z just gives me a noise indicating that I did something wrong, and the 'Undo' arrow is grayed out. Is there something I can do to fix that? FYI, I'm using Excel 2013.
Thanks,
Zach
Zach
Short answer:
No, that is a disadvantage with the macro.
Long answer:
https://stackoverflow.com/questions/24185942/save-undo-stack-during-macro-run
https://www.jkp-ads.com/Articles/UndoWithVBA00.asp
Thanks for your support Oscar.
In case i Want to apply this formula but to just specific columns... what do I have to change or add on this vba code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Application.ScreenUpdating = False
For Each Value In Target.Columns
Worksheets(Sh.Name).Rows(Value.Column).AutoFit
Next Value
Application.ScreenUpdating = True
End Sub
So far this works great for me, but how do you also have it adjust the height as well as the width in the VBA? So far it just the width. I have sheet with several columns and cells that auto fill from what is selected in the drop down list.