Written by Allen Wyatt (last updated September 18, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
When Robert double-clicks the right edge of a column header, Excel widens the column to fit the widest content of any cell in that column. Sometimes, though, that is just too wide. So, Robert wonders if there is a similar feature to double-click a cell edge and have the column widen to fit the contents of just that cell, even if there is a cell in the column with wider content.
There is no built-in double-click option to do this, but there is a way. Here's the simple steps:
The result is that the column width is set so that whatever is in the selected cell is entirely visible. If you prefer a keyboard-based approach, you can press the Alt key to trigger the hot keys for the ribbon tools, and then press H, O, I. If you prefer, you can use a legacy shortcut key sequence—again, press Alt to trigger the hot keys, and then press O, C, A. Either approach will work just fine.
If you prefer a macro-based approach, then you can use the following single-line macro:
Sub ColWidthCell() ActiveCell.Columns.AutoFit End Sub
The macro can, of course, be assigned to a shortcut key or to a customization of the ribbon so that it can be easily triggered.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11302) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
Are you a keyboard-only user wondering how you can set column width or row height without using the mouse? This tip ...
Discover MoreWant to hide a column so it doesn't appear in the worksheet? It's easy to do using the formatting capabilities of Excel.
Discover MoreWant to automatically hide some columns that don't meet a date criterion that you set? You can't do it automatically, but ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2021-09-18 15:27:52
J. Woolley
This Tip's macro works well when ActiveCell is not a merged cell. The following VBA is more general:
Sub SetColumnWidthByCell()
Dim rMA As Range, rCol As Range
Dim nC As Long, nW As Double
Set rMA = ActiveCell.MergeArea
nC = rMA.Columns.Count
If nC = 1 Then
ActiveCell.Columns.AutoFit
Else
rMA.UnMerge
ActiveCell.Columns.AutoFit
nW = ActiveCell.ColumnWidth / nC
For Each rCol In rMA.Columns
rCol.ColumnWidth = nW
Next rCol
rMA.Merge
End If
End Sub
See My Excel Toolbox: https://sites.google.com/view/MyExcelToolbox/
2021-09-18 11:20:51
Another macro approach could be:
Sub SetColWith()
'Shortcut key set : Ctrl+Shft+W
ActiveCell.Columns.ColumnWidth = InputBox("New column width:", "Adjust Column Width")
End Sub
2021-09-18 09:20:06
Tomek
You can also add "AutoFit Column Width" to Quick Access Toolbar. Once you find it on the Home Ribbon, just right click on it and select "Add to Quick access Toolbar"
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments