need code VBA to protect an excel file

rapidito78840

Board Regular
Joined
Apr 15, 2004
Messages
118
Hello:
I know they are programs to unprotec the sheet and to unprotect the book, but I know , they are a code in VBA to protect all the file, and this code do this:
when some one try to remove the password protection to the sheet and to the book, this code run and leave the file without work.
Please if somebody can share to me this code, its very apreciated, because I make an excel file and I need to share in other computer but I dont want to other people can unprotect the sheet and unprotect the book and then make changes and use this file.

thanks for your answers and Its very apreciated your help
regards
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
How's this:

<font face=Tahoma><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN>

<SPAN style="color:#00007F">Public</SPAN> ws <SPAN style="color:#00007F">As</SPAN> Worksheet
<SPAN style="color:#00007F">Const</SPAN> myPassword <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> = "password" <SPAN style="color:#007F00">'   Set Password here</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> ProtectAll()
    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> ws <SPAN style="color:#00007F">In</SPAN> ActiveWorkbook.Worksheets
        ws.Protect Password:=myPassword
    <SPAN style="color:#00007F">Next</SPAN> ws
    ActiveWorkbook.Protect Password:=myPassword
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> UnprotectAll()
    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> ws <SPAN style="color:#00007F">In</SPAN> ActiveWorkbook.Worksheets
        ws.Unprotect Password:=myPassword
    <SPAN style="color:#00007F">Next</SPAN> ws
    ActiveWorkbook.Unprotect Password:=myPassword
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

I'm not sure what you mean here:
when some one try to remove the password protection to the sheet and to the book, this code run and leave the file without work.
If someone tries to unprotect the workbook/sheets, the workbook closes without saving? Why not just set it as Read-Only for all users except you? If that would be acceptable, let me know and I'll post the relevant code.

Just note that Excel's protection is very weak and can be broken quickly by someone who wants in.

Hope that helps,

Smitty
 
Upvote 0
Mr Penny:

Code:
I'm not sure what you mean here: 
--------------------------------------------------------------------------------

quote: 
--------------------------------------------------------------------------------
when some one try to remove the password protection to the sheet and to the book, this code run and leave the file without work

yes, the sheet and the book have a password, but if you run another program to unprotect the sheet and to unprotect the book, the excel file its closed and without saving nothing....

for last:
please ( maybe you are :LOL: ) but when or how to put the code what you supply ? because I have 31 sheets on this file ?
thanks
 
Upvote 0
Mr. Penny:
Code:
If someone tries to unprotect the workbook/sheets, the workbook closes without saving? Why not just set it as Read-Only for all users except you? If that would be acceptable,

the reazon to not do this , its : in some areas on the protected sheets, anybody can put information and work on them, but in some areas not, if I do "read only" the excel file its not work in all sheets...
 
Upvote 0
You can prevent the user from event attempting to unprotect with:

<font face=Tahoma><SPAN style="color:#00007F">Sub</SPAN> DisableProtection()
    Application.CommandBars("Tools").Controls.Item("Protection").Enabled = <SPAN style="color:#00007F">False</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> EnableProtection()
    Application.CommandBars("Tools").Controls.Item("Protection").Enabled = <SPAN style="color:#00007F">True</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>

when or how to put the code what you supply ? because I have 31 sheets on this file ?
As for where to put the code, open the VB Editor (ALT+F11), then goto Insert-->Module and paste the code in the new window that opens on the right. You can call it from Tools-->Macro-->Macros or hit F5 from within any procedure. The first code will protect all of your worksheets regardless of how many there are.

Here is some code that will test for worksheet structure and if it's been unprotected it will close without saving (just read the comments):

<font face=Tahoma><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Workbook_SheetSelectionChange(<SPAN style="color:#00007F">ByVal</SPAN> Sh <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)
    <SPAN style="color:#007F00">'   If this works the way that you want it you can change the Message Box</SPAN>
    <SPAN style="color:#007F00">'   to indicate that the user is bad and the workbook will close wihout saving changes</SPAN>
    <SPAN style="color:#007F00">'   Then uncomment the ActiveWorkbook.Close False lines</SPAN>
    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> ActiveSheet.ProtectContents <SPAN style="color:#00007F">Then</SPAN>
        MsgBox "Sheet is not Protected", , "Protection Status"
        <SPAN style="color:#007F00">'   ActiveWorkbook.Close False</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> ActiveWorkbook.ProtectWindows <SPAN style="color:#00007F">Then</SPAN>
        MsgBox "Workbook Windows not Protected", , "Protection Status"
        <SPAN style="color:#007F00">'   ActiveWorkbook.Close False</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> ActiveWorkbook.ProtectStructure <SPAN style="color:#00007F">Then</SPAN>
        MsgBox "Workbook Structure is not Protected", , "Protection Status"
        <SPAN style="color:#007F00">'   ActiveWorkbook.Close False</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>

That particular piece of code goes in the ThisWorkbook module.

Smitty

EDIT: Note that if you're relying on code to keep people honest, then remove any additional temptation and protect the VBA project. In the VBE goto Tools-->VBA Project Properties-->Protection-->Lock Project for Viewing.
 
Upvote 0
Mr Penny:
I need to put the two codes?
Code:
Sub DisableProtection()
    Application.CommandBars("Tools").Controls.Item("Protection").Enabled = False
End Sub

Sub EnableProtection()
    Application.CommandBars("Tools").Controls.Item("Protection").Enabled = True
End Sub

and this too ?
Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    '   If this works the way that you want it you can change the Message Box
    '   to indicate that the user is bad and the workbook will close wihout saving changes
    '   Then uncomment the ActiveWorkbook.Close False lines
    If Not ActiveSheet.ProtectContents Then
        MsgBox "Sheet is not Protected", , "Protection Status"
        '   ActiveWorkbook.Close False
    End If
    If Not ActiveWorkbook.ProtectWindows Then
        MsgBox "Workbook Windows not Protected", , "Protection Status"
        '   ActiveWorkbook.Close False
    End If
    If Not ActiveWorkbook.ProtectStructure Then
        MsgBox "Workbook Structure is not Protected", , "Protection Status"
        '   ActiveWorkbook.Close False
    End If
End Sub

the second code its with the green letters ?

thanks
 
Upvote 0
Por favor, me llamo es "Smitty". "Mr. Penny" esta mi padre. :wink:

Regarding:
I need to put the two codes?
Not necessarily. The first snippet will disable the Protection option from the "Tools" menu bar; it's up to you to invoke that or not.

Regarding the second code snippet, it will alert you or a user if a workbook/sheet is not protected. The green parts are code comments; anything preceded by an apostrophe in VBA is ignored and viewed as a comment. Deleting the apostrophe activates the code (if it's valid code).

If you're having a hard time understanding what I'm (or anyone else) is posting, you may want to refer to this post in your post in the International Forum for translation.

I apoligize if my Spanish skills stink (yo hablo Espanol un poquito...I used to be pert good at West Texas Ranch Mexican though...).

There are many members who are competent in Spanish, unlike me.

My apologies if I confused you.

Smitty
 
Upvote 0
Mr. Smitty :
no you dont confuse to me, but I put all the codes what you supply, and with the program "advanced Office password recovery professional edition" all the password in all the sheets are removed and the vba code ( locked) its be cleared.

did you have another option.... because I follow all what you say to insert the codes lock on VBA, but with this program all password are removed and you can change anything.

PLEASE ANOTHER CHOICE ?

THANKS and sorry for the name....
 
Upvote 0
advanced Office password recovery professional edition
If you're defeating Excel's protection features using an external program, then there's pretty much nothing you can do. Excel is not a secure environment, nor has it ever been marketed as one.

Smitty
 
Upvote 0

Forum statistics

Threads
1,217,470
Messages
6,136,846
Members
450,027
Latest member
Apexwolf

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 MrExcel.com.
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 "mrexcel.com".
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
Back
Top