autofit cells in column width

B

Boethius1

i have a spreadsheet that i am sharing with someone, i want to leave two
blank columns for them to use, but i want to protect the rest of the
spreadsheet.

I do not know how wide they need the columns to be as this can change,
but if i protect the sheet they cannot widen the columns themselves.

Is there a way to set the columns to autofit?
----------------------------------------------------------:)
 
D

Dave Peterson

You could use a worksheet event.

The code unprotects the worksheet, autofits the columns and reprotects the
worksheet.

Rightclick on the worksheet tab that should have this behavior. Select view
code and paste this into the code window:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Me.Unprotect Password:="password"
Me.Range("d1,F1").EntireColumn.AutoFit
Me.Protect Password:="password"
End Sub

(I used column D and F--you could do more columns (or all of them).)

Change the password to what you need.
 
D

David

That works great for the cell where data is actually entered, but it does not
work for cells with formulas. Is there anything extra I can add to also auto
adjust cells with formulas?

Dave Peterson said:
You could use a worksheet event.

The code unprotects the worksheet, autofits the columns and reprotects the
worksheet.

Rightclick on the worksheet tab that should have this behavior. Select view
code and paste this into the code window:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Me.Unprotect Password:="password"
Me.Range("d1,F1").EntireColumn.AutoFit
Me.Protect Password:="password"
End Sub

(I used column D and F--you could do more columns (or all of them).)

Change the password to what you need.
 
D

Dave Peterson

You could use the worksheet_calculate event.

Option Explicit
Private Sub Worksheet_Calculate()
Application.EnableEvents = False
Me.UsedRange.Columns.AutoFit
Application.EnableEvents = True
End Sub


That works great for the cell where data is actually entered, but it does not
work for cells with formulas. Is there anything extra I can add to also auto
adjust cells with formulas?

Dave Peterson said:
You could use a worksheet event.

The code unprotects the worksheet, autofits the columns and reprotects the
worksheet.

Rightclick on the worksheet tab that should have this behavior. Select view
code and paste this into the code window:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Me.Unprotect Password:="password"
Me.Range("d1,F1").EntireColumn.AutoFit
Me.Protect Password:="password"
End Sub

(I used column D and F--you could do more columns (or all of them).)

Change the password to what you need.
 
D

David

Now you're talking!!
I left the password lines from your previous code in and just replaced the
middle line (for the specific columns) with the three lines for the whole
worksheet (and turning off/on display).

Thanks much! Exactly what I was looking for!!

Dave Peterson said:
You could use the worksheet_calculate event.

Option Explicit
Private Sub Worksheet_Calculate()
Application.EnableEvents = False
Me.UsedRange.Columns.AutoFit
Application.EnableEvents = True
End Sub


That works great for the cell where data is actually entered, but it does not
work for cells with formulas. Is there anything extra I can add to also auto
adjust cells with formulas?
 
Top