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

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.

madchemist

Board Regular
Joined
Jul 10, 2006
Messages
198
Figured it out right after I posted this

Needs to be Password:= "password"

Thanks for your reading time!
 
Upvote 0

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
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,190,590
Messages
5,981,816
Members
439,736
Latest member
Nathan20

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