Locking workbook on save/close???

jms493

Board Regular
Joined
Aug 6, 2003
Messages
58
I am saving a excel speadsheet on a network at work. Some people only need to view it and dont need to change anything. Some people will need to go in a change data. Problem I see is that when a person opens it up unprotects it and saves it, it doesnt prompt them to password protect it or does not do it automatically upon close.

So the next time someone opens it up it will be unprotected.

Can I get it to alway protect the worksheet upon closing witht he same password???

Thanks
 

Some videos you may like

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
You could use something along these lines:
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim Sh As Worksheet
    Dim myPassword As String
        myPassword = "password"
    
        For Each Sh In ActiveWorkbook.Worksheets
            Sh.Protect Password:=myPassword
        Next Sh
End Sub
The code goes in the ThisWorkbook module and will password protect each sheet when the wb is closed.

Hope that helps,

Smitty

EDIT: DOH! Sorry about that.
 

jms493

Board Regular
Joined
Aug 6, 2003
Messages
58
Can u fix that code for me it is quite messy and i am not a programmer.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,658
Messages
5,597,403
Members
414,142
Latest member
Banyangt

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
Top