VBA or Format to Expand all cells after each input

c0087

Board Regular
Joined
Jul 13, 2015
Messages
85
Office Version
  1. 365
Platform
  1. Windows
I want all cells in a worksheet to expand after every input as if you had selected them all, then double clicked the grid line to manually expand them. Thanks in advance
 
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

all the cells in each column are the same size, but not all columns are the same width
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
MAIT
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
nailed it BOSS. Much thanks to both you guys
 
Upvote 0
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.
 
Upvote 0
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.

It's about 250 cells so no performance problems so far. I don't know why your second solution was working for you and not me. Your first solution would've worked as my backup had I not been able to get my ideal solution. Thanks again for your time and help
 
Upvote 0
MAIT, 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
 
Upvote 0
MAIT, 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
Just tested this one out, but not all columns expanded. Your first solution still works the best (y)

*FWIW* My data set is around 1500 cells (with formulas) and it works almost instantly
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,053
Messages
6,122,888
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top