protect and unprotect workbook using vba code


Board Regular
Feb 10, 2011
i'm looking for a vba code to solve the following problem
a .xlsm workbook, the protect workbook passwork is known as "111111", the VBA project password is "222222", and how to write vba code to insert a new sheet, suppose the workbook has three worksheet, now using vba code to unprotect workbook, and auto insert a newsheet, then protect workbook again, whcih the password remains as "111111"
using a commondbutton to do this, everytime you click, insert a new sheet.

hope someone can help, many thanks!

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try like this

ThisWorkbook.Unprotect Password:="111111"
Worksheets.Add after:=Worksheets(Worksheets.Count)
ThisWorkbook.Protect Password:="111111"
Upvote 0
sorry, VoG, cann't working.
i think the first step is to unprotect the VBA project, and i want this to be write in vba code, 'cause i don't want someone else to konw the VBA project password, that means the code has to unprotect vba project, and then to unprotect workbook, after insert sheet/sheets, to protect workbook, at last to protect vba project.
this will give someone right to do some operation without know the workbook and vba project passwords.

anyway, VoG, thanks for your message!
Upvote 0
You don't need to Unprotect the Project to run the macro.
If you have access to the macro, and paste the code Peter provided, it will work....
Are you suggestiing you want a macro to Unprotect the Project AND Unprotect the workbook as well ??
If so, it can't be done via a normal VBA macro !
Upvote 0
hi, Michael

now, i have a new problem, after i protect the vba project(the password is "222222"), when i open the .xlsm workbook, it says cann't enable macros, the enable macros button disappear. the reason of this is because i protect the vba project, when i unprotect it, it works, Michael, could you please fix this problem and tell me why.
Thank you very much !
Upvote 0
As I said earlier, Protecting the Project has nothing to do with enabling / disabling macros.
Almost all of my projects are Project protected and they work fine !
So, no, I can't fix the rpoblem, nor can I advise on what to do, because I've never come across the problem before.
Upvote 0
I have searched for the code myself to unlock the vba project and was told that this is not something that you can do using script as it defeats the whole purpose of locking the VBA project, however, you can look up Hex editor which you can use to remove any passwords from the VBA project, I have used this when I locked one of MY OWN projects and forgot the VBA project password, this site does not promote hacking so be careful what you request.

I tried password removers but they only worked for the actual workbook password not the VBA project password.

This will not help you though as you want script to unlock the VBA project when using the VBA project.
Upvote 0
Suggesting ways or methods to break passwords is inappropriate and against forum rules.
Please refer to the first line of my post for a complete list of Forum rules.
If you need to "break" passwords you will have to look elsewhere for assistance
Upvote 0
No, files can't be uploaded to this site.
You will have to use a secondary provider like MediaFire, and then provide a link to that site.
But, a lot of posters won't look at downloaded files.
Upvote 0

Forum statistics

Latest member

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back