How to allow "column width change only" in a protected sheet? (As bonus, perhaps also auto-resize column width up to a max width?) VBA?

Rnkhch

Well-known Member
Joined
Apr 28, 2018
Messages
578
Office Version
  1. 365
Platform
  1. Windows
Hello,

In one of my protected sheets (only two options checked in protect dialog: "select locked cells" and "select unlocked cells"), I want to allow the users to only be able to change the width of columns F and H with no other additional capabilities allowed.

I tried checking the option for "format columns" as well when protecting the sheets, but this also allows the users to hide/unhide and perhaps allows doing other things as well that I don't know. So I'm trying to avoid this scenario.

So I was wondering if there is a way that in my protected sheets (with only the two initial options selected), is there a way to allow only column change (for F and H) and nothing else additional? I'm guessing this would require VBA code?

Also as an added bonus, would it be possible to allow automatic resizing of the width of columns F and H up to a maximum width of say 60? If possible, I was thinking to implement a user-editable dropdown switch (allow automatic column width? yes/no), and then if the user selects 'yes', the VBA code would switch to the automatic adjusting of the width based on the length of the longest cell in the column (up to 60). And if the user selects 'no', then they will be allowed to manually change the column width.

Thanks for any input! 🤗


P.S.
(Also please keep in mind that there are other VBA code in my fancy application 😅, and I would need this new code to coexist with the previous codes which can be found in the following posts:
)
 

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)
So, here's my input. There is no way to trap events for hiding or resizing a column and having vba from preventing that. You can trap events if a user selects an entire column, therefore you could prevent them from hiding the column. You could resize columns after any of these events: cell selection change, Cell contents change, Sheet selection change, and Table Update.
 
Upvote 0
I created a named range called AutoWidth and added a data validation with Yes,No. Add the code below to the SHEET level module in VBA


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim i As Range
  
  Set i = Intersect(Target, Union(Range("F:F"), Range("H:H")))
  If Not i Is Nothing Then
    If Range("Autowidth").Value = "Yes" Then
      Target.EntireColumn.AutoFit
      If Target.ColumnWidth > 60 Then Target.ColumnWidth = 60
    End If
  End If
  
  
End Sub
 
Upvote 0
Altering to include unprotecting the sheet when protected with a password

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim i As Range
  
  Set i = Intersect(Target, Union(Range("F:F"), Range("H:H")))
  If Not i Is Nothing Then
    If Range("Autowidth").Value = "Yes" Then
      ActiveSheet.Unprotect Password:="Jeff"
      Target.EntireColumn.AutoFit
      If Target.ColumnWidth > 60 Then Target.ColumnWidth = 60
      ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="Jeff"
    End If
  End If
  
  
End Sub
 
Upvote 0
Thank you! I was just trying it. How do I specify which cell the "Autowidth" yes/no is coming from?
 
Upvote 0
Select a cell. Add the data validation. Add a named range from the formula menu
 
Upvote 0
So I tried this over the weekend on my actual file, but couldn't get it to work. I was just trying a very simple version like you did in a new file, but it didn't work either. Likely I'm doing something wrong. Any chance you could share your sample file you made?

Thank you so much! 🤗
 
Upvote 0

Forum statistics

Threads
1,224,810
Messages
6,181,079
Members
453,021
Latest member
Justyna P

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