Password protect a worksheet using VBA

Broccoli

New Member
Joined
Mar 22, 2009
Messages
6
Hi there

Is there a way to password protect a worksheet using VBA?

I'm trying to password protect a sheet with the password "coffee".

However when I record and run a a macro to do this, the sheet is protected but without a password. Ie the VBA code does not state that the password must be coffee, it just protects the sheet without a password.

Below is the code the code generated by the macro:

ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
L

Legacy 68668

Guest
Just add a named argument, Password:=

ActiveSheet.Protect Password:= "coffee", DrawingObjects:=True, Contents:=True, Scenarios:=True
 
Upvote 0

Forum statistics

Threads
1,191,688
Messages
5,988,027
Members
440,125
Latest member
vincentchu2369

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