VBA for Protecting Workbook

James__S

Active Member
Joined
Jan 26, 2014
Messages
332
Hi,

Would anyone have VBA Code to lock a Workbook with 50+ tabs

1. Workbook can only be edited if correct password entered

2. If the person does not have the password then the Workbook is Read-Only

Thanks
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Maybe like this

Code:
Sub MM1()
Dim ws As Worksheet
For Each ws In Worksheets
    ws.Protect "password"
Next ws
End Sub
 
Last edited:
Upvote 0
Sorry....I thought locking the sheets may have been adequate...

Try

Code:
Sub MM1()
Dim pwd As String
pwd = InputBox("Please Enter the password")
If pwd = "" Then Exit Sub
ActiveWorkbook.Protect Structure:=True, Windows:=True, Password:=pwd
MsgBox "Workbook is now locked."
End Sub
 
Upvote 0
Sorry....I thought locking the sheets may have been adequate...

Try

Code:
Sub MM1()
Dim pwd As String
pwd = InputBox("Please Enter the password")
If pwd = "" Then Exit Sub
ActiveWorkbook.Protect Structure:=True, Windows:=True, Password:=pwd
MsgBox "Workbook is now locked."
End Sub

Hi michael

I entered the VBA into Workbook module but it still does not lock the workbook.

I ran the Code from Macros box and it opened up Password Box. i entered Password and locked. When i opened back up i could type in any cell
 
Upvote 0
Protect a workbook will prevent other users from viewing hidden worksheets, adding, moving, deleting, or hiding worksheets, and renaming worksheets
Protecting each sheet stops the user from entering deleting data...hence my first code !
 
Upvote 0
Thank you

Yes correct the VBA code below:

Is it possible with the code for the following
1. When the workbook is opened all sheets are locked and content read-only
2. When a person clicks on the cell in any sheet in the workbook a Pop-Up Message Box asks for a password to unlock. If password is incorrect than again workbook/worksheets are read-only
3. Workbook is automatically locked when closed
4. Can password be in code as this then can be changed if required

Should code be placed in module or Thisworkbook module

Thanks

Code:
Sub MM1()
Dim ws As Worksheet
For Each ws In Worksheets
    ws.Protect "password"
Next ws
End Sub
 
Last edited:
Upvote 0
If you want sheets to be unprotected / protected seperately you will need to place a code in each worksheet !

Code:
Sub MM1()
Dim pwd As String
pwd = InputBox("Please Enter the password")
If pwd = "" Then Exit Sub
ActiveSheet.Unprotect Password:=pwd
MsgBox "Worksheet is now open."
End Sub

You would then need a workbook close event to reprotect ALL sheets

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean) 
Dim ws As Worksheet
For Each ws In Worksheets
    ws.Protect "password"
Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

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