If I understand the question correctly, you will protect the sheet. If so, you can use the following VBA:
myWorksheet.Protect contents:=True, userinterfaceonly:=True
The key part here is "userinterfaceonly: = true". When a worksheet is protected by this set of flags, VBA macros are still allowed to make changes.
Put this code in the WorkBook_Activate event to automatically protect the book and set the flag when it is activated.
Edit: Thanks to Lance Roberts for his recommendation to use WorkBook_Activate instead of Workbook_Open .
Edit: Since the above does not work, you may have to wrap the damaged part of the VBA code with the protection / protection commands. If you do this, I would also wrap the entire macro with an error handler so that the sheet does not remain unprotected after the error:
Sub MyMacro On Error Goto HandleError ... myWorksheet.unprotect With ModifyCell.Validation ... End With myWorksheet.protect contents:=True, userinterfaceonly:=True ... Goto SkipErrorHandler HandleError: myWorksheet.protect contents:=True, userinterfaceonly:=True ... some code to present the error message to the user SkipErrorHandler: End Sub
Edit: See this thread on the PCreview page. They went through the same steps and came to the same conclusion. At least you are not alone!
e.James Jan 15 '09 at 15:44 2009-01-15 15:44
source share