In my spreadsheet I need to fit the column width automatically in Excel. If a user is entering a longest word that column should increase based on the word/integer automatically. I don’t want my user to increase the width of a column. For that I got a formula from Internet which is a method to make all columns, in all worksheets in the active workbook, auto fit if data is entered that exceeds the current column width.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim actCol, curCol
actCol = Target.Column
If actCol > 52 Then
curCol = Chr(Int((actCol - 1) / 52) + 64) & _
Chr(Int((actCol - 27) / 26) + 64) & _
Chr(Int((actCol - 27) Mod 26) + 65)
ElseIf actCol > 26 Then
curCol = Chr(Int((actCol - 1) / 26) + 64) & _
Chr(Int((actCol - 1) Mod 26) + 65)
Else
curCol = Chr(actCol + 64)
End If
Columns(curCol & ":" & curCol).AutoFit
End Sub
With your workbook open, press ALT + F11 (Function Key F11)
Double click 'THIS WORKBOOK' in the Microsoft Excel VBA Project objects in the upper left quadrant.
Paste the event handler into the white editing area to the right (right click inside the area and 'Paste').
Close the VBE (red button w/white 'x' --- top right).
Save the workbook.
I used this formula and it is working partially correct. But still I’m facing issue when the inputs given are showing as #### if the integer exceeds more than 4 character. How to fix this issue?
Screenshot:

Note: Since that file contains some confidential information I can't share the file here.