Hello WBA-1222,
Thank you for initiating a question in the Microsoft Community.
Unfortunately, there may be no keyboard shortcuts that make this easy to accomplish. But I think there are two ways to solve this problem.
Select all cells and set
After editing the contents of all cells, click on the triangle symbol in the upper left corner of the worksheet (as shown below) to select all cells, and then select Home -> Cells -> Format -> AutoFit Column Width in turn. after setting, the following result is achieved.
However, the disadvantage of this method is that, after setting the column width of the entire worksheet, if you edit a new cell, it will not adapt to adjust the column width, and you still need to set it for a specific cell or for all cells by the above method.
Macro
To automate the column width change after editing a cell, as you said, this automation requires a more advanced tool: macros.
Actually, macros are not that difficult to implement, and based on what I've given, it should be possible. If you think it's necessary, you can refer to the following steps and code, which, if run correctly, will give you better results in terms of auto-adaptation.
To save a worksheet with the auto-fit column feature as a template in Excel, follow these steps:
Step 1: Write the VBA Code
Add VBA Code to the Worksheet Module
- Open the VBA Editor by pressing Alt + F11.
- In the Project Explorer on the left, double-click the target worksheet (e.g., Sheet1).
- Enter the following code: Private Sub Worksheet_Change(ByVal Target As Range) Dim col As Range For Each col In Target.Columns col.EntireColumn.AutoFit Next col End Sub
Add VBA Code to the ThisWorkbook Module (if you want it to apply to the entire workbook)
- In the Project Explorer on the left, double-click the ThisWorkbook object.
- Enter the following code: Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) Dim col As Range For Each col In Target.Columns col.EntireColumn.AutoFit Next col End Sub
Step 2: Save the Workbook as a Template
- Return to Excel: Press Alt + F11 to return to Excel.
- Save as Template:
- Click the "File" menu.
- Select "Save As".
- Choose the location where you want to save the file (e.g., Desktop or Documents).
- In the "Save as type" dropdown menu, select "Excel Template (*.xltx)".
- Name the template and click "Save".
Step 3: Use the Template
- Create a New Workbook Based on the Template:
- Click the "File" menu.
- Select "New".
- Click the "Personal" tab (or "My Templates" depending on your Excel version).
- Choose the template you saved.
Notes
- Ensure macro security settings allow you to enable macros. In Excel, go to "File" -> "Options" -> "Trust Center" -> "Trust Center Settings" -> "Macro Settings", and select "Enable all macros" (only if you trust this template).
- When saving the template, if you need to share it with others, make sure their version of Excel is compatible with macros.
By following these steps, you can create an Excel template with an auto-fit column feature that automatically adjusts column width every time you create a new workbook.
Whichever method you choose, I respect your choice and I hope this has been helpful, if you still have questions, please feel free to ask.
Sincere greetings,
Ian - MSFT | Microsoft Community Support Specialist