ThisWorkbook.Protect in Workbook_open() not working 100% of the time

28 Views Asked by At

I have a ThisWorkbook.Protect function set at Workbook_open() event to make sure i don't leave it unprotected after working with it but it seems to me it doesnt work at all times.

I have made a line that displays MSGBOX "Witamy" inside the code which was my idea of checking if it's working.

Funny thing is that i get a msgbox but i still sometimes can delete sheets which i don't want my coworksers to be able to do.

Here is the code

Private Sub Workbook_Open()

Call SetPass 'sets iPass variable

ThisWorkbook.Protect Password:=iPass

For Each sh In Worksheets
sh.Protect Password:=iPass, UserInterFaceOnly:=True
Next sh

Sheets("Strona główna").Activate

MsgBox "Witamy"
End Sub

Note: Loop also works 100% of the time or at least i didn't notice it not working. Note2: Sometimes if it doesn't work and i close and reopen Workbook it works again

Any idea of how to fix the issue or diffrent approach of preventing leaving workbook unprotected?

0

There are 0 best solutions below