I already know how to hide macro code from the user; Simply open up your VBA project and go Tools
- VBAProject Properties
- Protection
and check Lock project for viewing
enter a password and your code is made inaccessible to users.
I want to know if this can be automated with VBA.
Why?
I have a sheet of data that I use a macro to scramble, and a key to unscramble. A macro runs on a Workbook_open
event and I want to guarantee this runs before the user can access any content.
When a user opens the workbook, they can just hold Shift or choose not to enable macros, I don't want that. So I hide the macro code and protect all the cells by default using a really long and strong password. Part of the Workbook_open
code then removes these restrictions (presumably by using the password), and of course the Workbook_open
code only runs if macros are enabled.
So can I toggle the viewing status with VBA? I imagine some string of SendKeys
would do the job, but not ideal.
xlVeryHidden
, and have yourOpen
event unprotect and make itxlVisible
. That's the best you can do. In that case if the macros are disabled, the user won't be able to do anything until/unless she hacks your VBProject, and if she's that competent, there's nothing you can do anyways. – David Zemens