Locking the VBA code from VBA

Is there a way that you can run a Sub in VBA that will set a password on the code and lock the code for reading?

Thanks
GordonMassonAsked:
Who is Participating?
 
Rory ArchibaldConnect With a Mentor Commented:
Here you go.
Project-Lock-test2.xls
0
 
Rory ArchibaldCommented:
See this page for example: http://www.standards.com/Office/SetVBAProjectPassword.html
I don't know how reliable it is, but probably more reliable than the alternative (SendKeys)
0
 
jppintoCommented:
Locking the VBA code is not 100% secure. There is always ways to see the code. But you can go to the VBEditor, on Tools-VBA Project properties, go the Protection tab on the dialog that opens, check the 'Lock project for viewing' option and enter a password. Save the file, close Excel and reopen the file.

jppinto
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
GordonMassonAuthor Commented:
Thanks jppinto

I thought that locking the code did make it secure?? How can you view the code if it is locked?

I know that I can use the method you have explained but I want to do that automatically rather than have to go through the manual method.
I have a worksheet that has lots of VBA behind it and I don’t want it being altered.
One of the Subs deletes some pages and reformats the worksheet to make it available for distribution. I would like to include an automatic lock of the code at this point.
0
 
Rory ArchibaldCommented:
The point is that it's easy to break if you know what you're doing, not that it doesn't work.
0
 
jppintoCommented:
Yes, I agree with rorya. That's exactly my point!
0
 
jppintoConnect With a Mentor Commented:
Also you might want to take a look at this MS article:

http://msdn.microsoft.com/en-us/library/aa165442(v=office.10).aspx

jppinto
0
 
GordonMassonAuthor Commented:
Rorya

I had a look at your link…. http://www.standards.com/Office/SetVBAProjectPassword.html  but I am afraid converting this from VB to VBA is outwith my knowledge.

Ippinto
The example you posted …http://msdn.microsoft.com/en-us/library/aa165442(v=office.10).aspx  ….refers to a Word document and I am not sure how this translates into an Excel version.

Is it possible to provide me a sample worksheet that either uses rorya’s method or ippinto’s method please?

Thanks for your help.
0
 
GordonMassonAuthor Commented:
Thanks rorya

Perhaps i am missing something here but the new sheet this creates doesn’t appear to be locked?
Am i doing something wrong. It does come up with the "The project was locked" msg box.

Is there any way to run this code from the project you are actually trying to lock.

thanks for your help
Very much appreciated.
0
 
Rory ArchibaldCommented:
It will only be locked once you close and reopen it, just as when you do it manually. The test sub at the bottom is only there to demonstrate usage - you can lock any project you specify.
0
 
GordonMassonAuthor Commented:
I have managed to get this to work on the sample code that has been posted but not in my own worksheet yet.

this is probably something i am doing wrong but i will update you as soon as i have checked this again.

Thanks for your help and sorry for the delay.
0
 
GordonMassonAuthor Commented:
Thanks for your help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.