I have written a VBA macro to do some stuff in Excel, and I want to know if the following is possible.
How do I protect a Macro so that no matter who uses the spreadsheet, they cannot edit the code? The protection needs to be strong enough that even if they press ALT + F11 and pull up the macro, the Edit button options need to be greyed out, even if the macro has been selected.
Also is it possible to stop a user from even seeing the code?
I think the easiest way is just to protect the workbook code. VBA Project properties -> Protection -> Lock project for viewing. You will need to specify a password.
It's not world-class protection, but it will stop the average user editing the code.
Related
I'm setting up a shared Excel spreadsheet with macros, but I don't want to let anyone edit the document at all until they've clicked the "Enable Content" in the SECURITY WARNING bar at the top.
How can I "protect" the workbook like that?
Set up the workbook to run a macro before the file is saved (workbook.beforesave). In that routine, set all sheets to xlVeryHidden, except for one sheet where you explain that the user needs to enable macros. You could even have a screenshot showing what they need to click.
When macros are enabled, the workbook_open event can unhide all sheets you want to show.
Protect the VBA project with a password.
Someone with very malicious intent and advanced coding skills will probably be able to find a way to circumvent this setup, but it should work well in a business environment where people just want to get their work done.
I need to disable the copy/paste in excel 2007. The excel file will be sent to different users so i need a solution where copy/paste is disabled for all users.
I have searched the web but everyone has asked to enable the macros but the problem with that is i will have to enable the macros for every user first before restricting them from copy/pasting.
So looking for a solution where i can write some vba code on sheet to restrict all users from copy/pasting without enabling macros.
Thanks.
Lock the Cells with the Protection Option of Excel manually or lock them programmatically.
Example:
'Range Lock
Worksheets("Sheet1").Range("A1:G37").Locked = True
'Sheet Lock
Worksheets("Sheet1").Protect
EDIT:
Just saw ur new comment, there is no option to use VBA code to block ONLY copy/pasting without allowing Macros in ur Workbook.
You don't need VBA for this.
Go to the review tab
Select protect sheet
Protect with password and take away the ✓ (there should only be one)
I know that a vba macro can be password protected. I was wondering if the same could be applied to an event change macro? If yes then how would you go about it.
"To protect your code, open the Excel Workbook and go to Tools>Macro>Visual Basic Editor (Alt+F11). Now, from within the VBE go to Tools>VBAProject Properties and then click the Protection page tab and then check "Lock project from viewing" and then enter your password and again to confirm it. After doing this you must save, close & reopen the Workbook for the protection to take effect."
Source: http://www.ozgrid.com/VBA/protect-vba-code.htm
This was an easy google, I reccomend you look online before asking on Stack Overflow, these simple questions are generally not well received.
If your question is about protecting a specific part of your code, and not the other parts of the code, then it can't be done as far as I know.
I have a workbook, which creates a second workbook (using ThisWorkbook.SaveCopyAs) to present the data nicely to users. It relies on macros for part of the processing.
However, I've been asked to make those macros inaccessible to users. They still need to run them, but to view/edit them should require a password. I can figure out how to do it using the GUI (VBA Editor -> right click VBAProject -> VBAProject Properties -> Protection, tick the box and enter a password), but I haven't found a way to do so using VBA. The Workbook.Protect function seems to lock down everything except VBA.
If I try to apply it to the source workbook, I get "Can't perform operation since the project is protected", so that doesn't work either.
I'm running 2010, but the workbook needs to be compatible with 2003, so no fancy new tricks. :(
This is a method that describes setting the project password without using sendkeys http://www.standards.com/Office/SetVBAProjectPassword.html
This post may be helpful to you. Note that in the comments it says you need to add vbeext1.olb.
I had a similar problem. I needed excel code to populate user created new sheets, but I wanted to password protect the project. It would work great as long as I did not use password proection, but Excel will not allow it to generate code in the new sheets with password protection.
The solution was interesting. Give your project an easy name or initials . Something people can find with ease.
You password protect your entire project and save the resulting file as an XLA (add on).
Then UNprotect the workbook, rename the file (just in case you make a mistake) and strip it of all the code, modules, classes, forms, etc. Save the new stripped Excel File.
Now open the new stripped file and add the xla in TOOLS/add ons. Then go to the VBA editor, References and find your project wiht a new name, and link (you may have to save and reopen once to find under referneces).
That's it. the new file, empty of all code, will operate using the old code but you will have no access to it without a password. At the same time, the old code can place the code on the new worksheets as before since the new workbook Project is not password protected
What I have found is that if you change locations, your users may need to find the link in the references, but I am sure you can create a macro to find and link automatically (that macro, of course, would be visible to the world).
I think this will solve any problem with protected code.
Good luck
I've created a worksheet which is shared amongst my colleagues with a real simply macro to show/hide rows but everytime either I or one of my colleagues opens it they get prompted to Allow marco - is there anyway for them to permantly allow this macro?
You'll want to digitally sign the project. Microsoft's information about how to do that is here: http://office.microsoft.com/en-us/excel-help/digitally-sign-a-macro-project-HA001231781.aspx
You need to change a setting in Macro Security within Excel so that your colleagues don't have to respond to a dialog box every time. Be advised, though, that when you allow macro code to run automatically, this can unsuspectingly open you up to malicious code in other Excel workbooks.
The location of the settings for Macro Security is different from versions 2003 and 2007 of Excel. In 2003 and previous, I believe the setting is visible in Tools > Macros > Macro Security. From there, you would change the security level to low. In Excel 2007 (and probably 2010), the setting is visible in the Developer tab on the ribbon (which you might have to make visible by toggling a checkbox in Excel Options). From the Developer tab, click Macro Security and chose Enable All Macros.
The above is the simplest way for your colleagues to make a change to their system, but a better way, as others suggest, would be to digitaly sign your own work so that your colleagues don't have to open a potential security hole on their own system.