Locking and Unlocking using VBA

madchemist

Board Regular
Joined
Jul 10, 2006
Messages
198
I have a userform that translates information into a row. I want to protect certain columns in the row to prevent users from accidentally changing data.

I know I need to place an .Unprotect and .Protect command in my macro to be able to let the macro enter information, but keep getting errors when I try and put a password in the code.

This is the macro I recorded:

Code:
ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:=True, AllowSorting:=True, AllowFiltering:=True

In the past, I have always just used the following to lock:

Code:
ActiveSheet.Protect Password = "password"

How do I append a password to the protect command with the criteria after it? Because if I do either of the following I get errors.....

Code:
ActiveSheet.Protect  Password = "password" DrawingObjects:=False, Contents:=True, Scenarios:=True, AllowSorting:=True, AllowFiltering:=True

Code:
ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:=True, AllowSorting:=True, AllowFiltering:=True Password = "password"


Am I missing a comma or something? Thanks!
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Figured it out right after I posted this

Needs to be Password:= "password"

Thanks for your reading time!
 
Upvote 0
Not a comma but a colon.
Code:
ActiveSheet.Protect  Password:= "password" DrawingObjects:=False, Contents:=True, Scenarios:=True, AllowSorting:=True, AllowFiltering:=True
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,202
Members
448,554
Latest member
Gleisner2

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