?
Solved

Locking the VBA code from VBA

Posted on 2011-05-12
12
Medium Priority
?
323 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:GordonMasson
  • 5
  • 4
  • 3
12 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35746041
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
 
LVL 33

Expert Comment

by:jppinto
ID: 35746055
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
 

Author Comment

by:GordonMasson
ID: 35746087
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35746111
The point is that it's easy to break if you know what you're doing, not that it doesn't work.
0
 
LVL 33

Expert Comment

by:jppinto
ID: 35746123
Yes, I agree with rorya. That's exactly my point!
0
 
LVL 33

Assisted Solution

by:jppinto
jppinto earned 600 total points
ID: 35746261
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
 

Author Comment

by:GordonMasson
ID: 35767518
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
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 1400 total points
ID: 35767840
Here you go.
Project-Lock-test2.xls
0
 

Author Comment

by:GordonMasson
ID: 35768274
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
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35768360
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
 

Author Comment

by:GordonMasson
ID: 35877079
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
 

Author Closing Comment

by:GordonMasson
ID: 36095668
Thanks for your help
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question