Sheet Protect (User Interface only) using VBA

Generally, if you want your vba code to run on a protected sheet, you wrap your code in unprotect/protect statements in every procedure or function. You also have to remember to protect the sheet in case it is unprotected.
Here is the attached file that clearly explains how you can avoid this. The following code in the Workbook_Open procedure/event protects all the sheets from the user but allows the macro code to work on the sheets.
There is no command for protection used anywhere in the workbook other than used in the Workbook_Open procedure as above. The code behind the two buttons on the sheet doesn't protect or unprotect the sheet for the respective codes to run.
Download the attached excel file and see how this works.
Private Sub Workbook_Open() Dim wks As Worksheet For Each wks In ThisWorkbook.Worksheets wks.Protect Password:="abc", UserInterfaceOnly:=True Next wks End Sub
| Attachment | Size |
|---|---|
| SheetProtectionUserInterface.xls | 32 KB |
»
- Vishesh's blog
- Add new comment
- 3522 reads

Recent comments
6 hours 15 min ago
9 hours 38 min ago
10 hours 1 min ago
16 hours 3 min ago
16 hours 55 min ago
1 day 7 hours ago
1 day 7 hours ago
1 day 11 hours ago
1 day 19 hours ago
2 days 14 hours ago