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
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try this:
Assuming your manually entering these values

This is an auto sheet event script
Your Workbook must be Macro enabled
To install this code:
Right-click on the sheet tab
Select View Code from the pop-up context menu
Paste the code in the VBA edit window

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  6/4/2019  2:14:17 AM  EDT
Target.Columns.AutoFit
End Sub
 
Last edited:
Upvote 0
Try this:
Assuming your manually entering these values

This is an auto sheet event script
Your Workbook must be Macro enabled
To install this code:
Right-click on the sheet tab
Select View Code from the pop-up context menu
Paste the code in the VBA edit window

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  6/4/2019  2:14:17 AM  EDT
Target.Columns.AutoFit
End Sub

It works, but only for the column I'm inputting data into. Anything I can edit in the code to get it to do the entire worksheet?
 
Upvote 0
Try this:
Code:
Sub AutoFit()
'Modified  6/4/2019  2:45:02 AM  EDT
Application.ScreenUpdating = False
Dim r As Range
For Each r In ActiveSheet.UsedRange
    r.Columns.AutoFit
Next
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Try this:
Code:
Sub AutoFit()
'Modified  6/4/2019  2:45:02 AM  EDT
Application.ScreenUpdating = False
Dim r As Range
For Each r In ActiveSheet.UsedRange
    r.Columns.AutoFit
Next
Application.ScreenUpdating = True
End Sub
No - it just starts with everything tight, and never expands afterwards
 
Upvote 0
When you run the last script I gave you it should autofit all cells on your sheet.

Are you saying the last script I sent you does nothing?
It works for me.
 
Upvote 0
You need to run the second script I gave you to set all the columns to Autofit
Then use the first code I sent you from now on.

Or please explain what your trying to do.

If you had a entire sheet full of data and you wanted all columns auto fitted my last script should work.

Now if you want the second script to run every time you enter a value into any cell in the entire sheet that could be done but then you might say my sheet is slowing down with performance issues.

If you have a sheet with a lot of data performing any script over and over every time you enter any value in any cell may slow down how fast Excel works.
 
Upvote 0
When you run the last script I gave you it should autofit all cells on your sheet.

Are you saying the last script I sent you does nothing?
It works for me.
It autofits everything first (immediately after i run it), then when i input values in the worksheet it does not auto-fit anything. Your first solution will work for what I need (much appreciated), but it would be ideal if i could get it to Auto fit all the columns after every input, rather than just the column I'm inputting data into.
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,224,748
Messages
6,180,721
Members
452,995
Latest member
isldboy

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