I assume you know all cells in a column must be the same width.
Range("A1") cannot be 2 mm wide and Range("A3") be 6mm wide
nailed it BOSS. Much thanks to both you guysMAIT
Maybe the OP hasn't modified your last snippet to be a Worksheet_change event.
Also try
Code:Private Sub Worksheet_Change(ByVal Target As Range) 'Modified 6/4/2019 2:14:17 AM EDT With ActiveSheet.UsedRange .Columns.AutoFit End With End Sub
Well this does work but your putting your worksheet to work for sure.
If you have 30,000 cells with data this script will have to run through all 30,000 cells every time you change any value on your sheet. So you may experience some performance issues.
But if your happy then that's what counts.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lc As Long
lc = Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
With ActiveSheet.Range(Cells(1, 1), Cells(1, lc))
.Columns.AutoFit
End With
End Sub
Just tested this one out, but not all columns expanded. Your first solution still works the bestMAIT, good comment...I assumed a reasonable dataset wouldn't be an issue.
This might be better, although I haven't tested for speed !!
Code:Private Sub Worksheet_Change(ByVal Target As Range) Dim lc As Long lc = Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column With ActiveSheet.Range(Cells(1, 1), Cells(1, lc)) .Columns.AutoFit End With End Sub