VBA cells protections

Skell2233

New Member
Joined
Nov 22, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello everyone,

I'm having a problem with protecting cells via VBA and I hope you can help me.

I have the following code written, which protects locked cells with password.
However, the issue is that it is still possible for users to manually unprotect the cells without password via review-unprotect sheet.

Can someone please help to explain why the password protection is not working properly?

Dim ws As Worksheet
Set wsarray = Sheets(Array(xxx"))

For Each ws In wsarray
ws.EnableOutlining = True
ws.Protect UserInterfaceOnly:=True
ws.Protect Password = "pwd"
ws.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowFiltering:=True, AllowUsingPivotTables:= _
True
Next
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
You should be specifying all the arguments in one protect statement, and the syntax is Password:="pwd" and not Password = "pwd":

VBA Code:
ws.Protect UserInterfaceOnly:=True, Password:="pwd", DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowFiltering:=True, AllowUsingPivotTables:= _
True
 
Upvote 0
Solution
You should be specifying all the arguments in one protect statement, and the syntax is Password:="pwd" and not Password = "pwd":

VBA Code:
ws.Protect UserInterfaceOnly:=True, Password:="pwd", DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowFiltering:=True, AllowUsingPivotTables:= _
True
Hi Rory,

the code works now, thank you very much for a quick reply.
 
Upvote 0
Glad we could help, and welcome to the forum! :)
 
Upvote 0

Forum statistics

Threads
1,213,550
Messages
6,114,265
Members
448,558
Latest member
aivin

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