I searched about the said problem with various keywords but all results were either related to how to protect a workbook or unprotect it without knowing the password.
My current problem is not really a big problem but it feels so weird that I want to know if other people had/is experiencing it.
I am currently making Excel 2010 VBA Macros to automate data processing. I am adding workbook protection so that users cannot move the position of worksheets. I have done this via VBA 'thisworkbook.protect' or through the traditional Excel menu.
When I run the following code, the workbook protection completely disappears regardless of passwords, structure/window settings.
Sub test()
ThisWorkbook.Worksheets(1).Cells(1, 1).Value(11) = ThisWorkbook.Worksheets(3).Cells(1, 1).Value(11)
End Sub
Just adding 'thisworkbook.protect' at the end of the code relocks the workbook and solves the problem, which is why this isn't a big problem. But the phenomenon is completely incomprehensible to me.
I appreciate anyone who would give feedback. Thanks.
edit 1 I believe this is not a worksheet.protect problem. My problem occurs regardless of whether worksheets and cells are protected, and I am specifically concerned about worksheet positions and names, which fall into the workbook.protect command.
Edit 2:
So the issue is in following and doesn't concern the difference between Workbook.Protect
and Worksheet.Protect
methods:
1. I open a protected workbook;
2. Run provided code snippet without unprotecting the workbook, as it doesn't affect worksheet's protection;
3. Workbook protection is cancelled by previous action despite my intentions. Have to protect the workbook again.
Workbook.Protect
Method protects the workbook from user interaction - overall workbook related actions (e.g. adding/deleting/moving sheets) which are sent from UI, but not from code. In order to protect sheets you have to use aWorksheet.Protect
Method which works as it should for code raised changes as well. – Vitaliy Prushak