Auto Column Width Macro/Protected vs. Unprotected Worksheet

Mei_Mienzi

New Member
Joined
Apr 11, 2016
Messages
20
Hello:

I have a spread sheet that I have made a macro for. That macro is a follows:

Private Sub Worksheet_Change(ByVal Target As Range)
Columns().AutoFit
End Sub

The two boxes above the macro are:
(Left Box) Worksheet (Right Box) Change

The macro works just fine, BUT I do have a problem. In order for the macro to run the worksheet has to be "unprotected." Is there any way to get this macro to run with the worksheet "protected?" If I leave it unprotected someone is going to louse it up. If left as is, in "protected" status, the macro will not run.

Any resolution?

Thanks,
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Welcome to the Board!

You can Unprotect/Reprotect in your code:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  ActiveSheet.Unprotect "PasswordHere"  
    Columns().AutoFit
  ActiveSheet.Protect "PasswordHere"
End Sub

HTH
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

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