Sheet protection

  • Thread starter Thread starter Marcelo Rychter
  • Start date Start date
M

Marcelo Rychter

Hi folks,

I used the code below to protect a sheet:

ActiveSheet.EnableSelection = xlNoSelection
ActiveSheet.Protect Password:="CADEMP"

It works properly. I tested it after running the code.

But when I save the worksheet and reopen it I CAN select the cells. It
seems that the code was not effective.

But when I do the same procedure using the Excel menu
(Tolls/Protect/...) and save, when a reopen the woksheet it works
properly.

Do some of you know how to avoid the problem?

Thanks,
Marcelo Rychter
 
Hi Marcelo,

The EnableSelection property is not persistent.

Try, therefore:

'=============>>
Private Sub Workbook_Open()
Dim SH As Worksheet

Set SH = Me.Worksheets("Sheet1") '<<==== CHANGE

With SH
.EnableSelection = xlNoSelection
.Protect Password:="CADEMP"
End With

End Sub
'<<=============

This is workbook event code and should be pasted into the workbook's
ThisWorkbook module *not* a standard module or a sheet module:

Right-click the Excel icon on the worksheet
(or the icon to the left of the File menu if your workbook is maximised)

Select 'View Code' from the menu and paste the code.


---
Regards,
Norman


"Marcelo Rychter"
 
Back
Top