How to make any sheet as password protected via VBA code

angeld27

New Member
Joined
Mar 25, 2011
Messages
5
Hi All,

Could you please help me on below query?

I have 5 worksheet, out of them i want 2 should be password protected.

Is there any VBA code to perform this operation?

Thanks
Ankit
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
You could try code like this

Code:
Sub prtct()
Dim ws As Worksheet
For Each ws In Worksheets(Array("Sheet2", "Sheet4"))
    ws.Protect Password:="abc"
Next ws
End Sub
 
Upvote 0
Hi Peter,

In this case i have to manually go to Review tab and enter the password for that particular sheet in order to unlock.

Do we have any option where we can click on a particular button and it ask for password to unlock it?
 
Upvote 0
Try assigning this to a button

Code:
Sub unprtct()
Dim ws As Worksheet, pw As String
pw = InputBox("Enter password")
If pw <> "abc" Then Exit Sub
For Each ws In Worksheets(Array("Sheet2", "Sheet4"))
    ws.Protect Password:=pw
Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,525
Messages
6,179,314
Members
452,905
Latest member
deadwings

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