简体   繁体   中英

Excel protect and unprotect workbook VBA

I have a workbook that contains several (lookup) tabs that I don't want any users to be able to unhide. However, there is one tab that contains admin info that I would like admin users to be able to see. I have a button and macro that prompts for an admin password before unhiding this tab, but if I protect the workbook structure (so that hidden tabs are not accessible for other users) the vba fails. Is there no way to protect/unprotect the workbook from within the admin macro? I have tried various combinations of ActiveWorkbook.Unprotect , ActiveWorkbook.Protect and ActiveWorkbook.ProtectStructure to no avail - both within the admin macro and WorkbookOpen. The error msg I most commonly encounter is "Can't assign to read-only property" even after having ActiveWorkbook.Unprotect at the top of my code. Does anyone know how to work around this? I don't really want to have to leave all tabs open to being unhidden with a second password on the Admin worksheet itself - clumsy!

Instead of protecting the whole workbook, set the Visible property of the sheets you want to hide to xlSheetVeryHidden - you can do this manually in the properties window of the VBA editor. Now those sheets won't show up in the Unhide… dialog. Your macro could swap the visibility of your admin-only sheet from xlSheetVeryHidden to xlSheetVisible and back.

By itself this doesn't protect your workbook from a user who knows how to access the VBA editor and properties, but if you need to do that there seem to be a lot of answers to a quick web search - it may depend on your version of Excel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM