I largely agree with my respected colleague @Lupp and recommend that you heed his advice. I don’t believe that hip-hop will be permanent. However, I warn you about another danger: if the text in the cell is too long, then the automatic width can make the column too wide.
indeed, the solution to your problem is to use a macro. For example, like this:
Option Explicit
Function SetOptimalWidth(Optional oDummy As Variant) As String
Const MAX_WIDTH = 10000
Dim oCurrentController As Variant
Dim oActiveSheet As Variant
Dim oCursor As Variant
Dim oColumns As Variant
Dim oColumn As Variant
Dim i As Long
Rem First of all, we find out the active sheet
oCurrentController = ThisComponent.getCurrentController()
oActiveSheet = oCurrentController.getActiveSheet()
Rem In order not to change all the columns on the sheet, we find the UsedRange
oCursor = oActiveSheet.createCursor()
oCursor.gotoEndOfUsedArea(True)
oColumns = oCursor.getColumns()
Rem Now we optimize the width of each column in a given range
For i = oColumns.getCount() - 1 To 0 Step -1
oColumn = oColumns.getByIndex(i)
oColumn.OptimalWidth = True
Rem We could stop at this
Rem But some columns may become so wide that they won’t fit on the screen
Rem In this example, we will limit the maximum width to 10 cm
If oColumn.Width > MAX_WIDTH Then
oColumn.OptimalWidth = False
oColumn.Width = MAX_WIDTH
EndIf
Next i
SetOptimalWidth = "Clear this cell if you want to stop the automatic column width"
End Function
Put this code in the Standard library in My Macros & Dialogs
Now put the formula =SETOPTIMALWIDTH(NOW())
in any cell of the sheet.
Using the NOW() function as parameter will cause the macro to run after each change on the current sheet. Moreover, this macro will also be executed if you press F9 (or choose Data - Calculate - Recalculate) on any other sheet of the spreadsheet.
This simple trick allows you not to use explicit sheet events that a colleague suggests exploring. The formula is very simple to insert into any cell and is just as easy to remove when the need for this automation disappears
Update In order not to use complex solutions using macros, assign a convenient hotkey to the “Optimal Column Width, direct” command and use it (Choose Tools - Customize - Keyboard tab)