VBA or Format to Expand all cells after each input

c0087

Board Regular
Joined
Jul 13, 2015
Messages
94
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

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
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 :beerchug:

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

Forum statistics

Threads
1,223,719
Messages
6,174,087
Members
452,542
Latest member
Bricklin

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