SameerMirza
asked on
Excel vba activesheet.protect/unprotect
hi,
Can any please tell me what exactly does activesheet.protect/unprot ect doeS?
Understanding is that its used to unprotect a protected worksheet and vice versa
But when is it used? and how/why?
The code that I am looking at calls,
activesheet.unprotact before writing to the excel file and at the end of sub again,
activesheet.Protect
Just wondering why its used coz only the vba module is protected.
Thanks
Can any please tell me what exactly does activesheet.protect/unprot
Understanding is that its used to unprotect a protected worksheet and vice versa
But when is it used? and how/why?
The code that I am looking at calls,
activesheet.unprotact before writing to the excel file and at the end of sub again,
activesheet.Protect
Just wondering why its used coz only the vba module is protected.
Thanks
Don't you have the Excel sheet protected? Usually this is used on a protected sheet to unprotect it so that you can make same changes and at the end of the changes, protect the sheet again. Are you sure you don't have nothing on you sheet that is protected?
Since Excel 2000 there has been an interesting additional attribute that allows the worksheet to be protected while allowing VBA to still work with the worksheet.
When a sheet is protected, Excel not only prevents the user from changing locked worksheet elements, it also prevents VBA from changing them. This is circumvented by unprotecting the sheet, making the changes, and then protecting the worksheet again as illustrated below.
Sheets("Sheet1").Unprotect Password:="password"
...
Sheets("Sheet1").Protect Password:="password"
An alternative and more efficient technique is to use the UserInterfaceOnly option on the Protect method. This option protects the sheet from manual changes the same as without the option, but it allows VBA access to the sheet's elements as if the sheet was unprotected. The only drawback is that the option setting cannot be set from the Excel application user interface and it is not saved with the workbook. This means that it has to be reset every time the workbook opens. This is accomplished by placing the code to set the option in the Workbook_Open or similar routine.
Private Sub Workbook_Open()
Sheets("Sheet1").Protect Password:="password", UserInterfaceOnly:=True
Sheets("Sheet2").Protect Password:="password", UserInterfaceOnly:=True
End Sub
Note that protection does not need to be turned off to make the change. The above code can be placed anywhere in the project as long as the UserInterfaceOnly setting is adjusted before the first attempt is made to make change a sheet element.
The UserInterfaceOnly option is available starting with Excel 2000.
Note that even with UserInterfaceOnly set to True, certain operations cannot be performed such as
- adding drawing objects and charts to the worksheet,
- turning AutoFilter on and off, and
- adding data validation to cells.
When these operations must be perfomed on a worksheet, the worksheet must be unprotected and protected again. Any operation attempted on a worksheet protected with the UserInterfaceOnly option that is not allowed will generate a 1004 error.
Kevin
When a sheet is protected, Excel not only prevents the user from changing locked worksheet elements, it also prevents VBA from changing them. This is circumvented by unprotecting the sheet, making the changes, and then protecting the worksheet again as illustrated below.
Sheets("Sheet1").Unprotect
...
Sheets("Sheet1").Protect Password:="password"
An alternative and more efficient technique is to use the UserInterfaceOnly option on the Protect method. This option protects the sheet from manual changes the same as without the option, but it allows VBA access to the sheet's elements as if the sheet was unprotected. The only drawback is that the option setting cannot be set from the Excel application user interface and it is not saved with the workbook. This means that it has to be reset every time the workbook opens. This is accomplished by placing the code to set the option in the Workbook_Open or similar routine.
Private Sub Workbook_Open()
Sheets("Sheet1").Protect Password:="password", UserInterfaceOnly:=True
Sheets("Sheet2").Protect Password:="password", UserInterfaceOnly:=True
End Sub
Note that protection does not need to be turned off to make the change. The above code can be placed anywhere in the project as long as the UserInterfaceOnly setting is adjusted before the first attempt is made to make change a sheet element.
The UserInterfaceOnly option is available starting with Excel 2000.
Note that even with UserInterfaceOnly set to True, certain operations cannot be performed such as
- adding drawing objects and charts to the worksheet,
- turning AutoFilter on and off, and
- adding data validation to cells.
When these operations must be perfomed on a worksheet, the worksheet must be unprotected and protected again. Any operation attempted on a worksheet protected with the UserInterfaceOnly option that is not allowed will generate a 1004 error.
Kevin
ASKER
thanks Kevin and jppinto
Jppinto,
thats exactly what I am thinking but I am not realy an excel guru so not sure if its protected but the idea is that if I can see or anything without a pass then its not protected now I can tell exactly where the records have been written but sure how to check if its protected
Idea is that it could just be a preventive active
Can you please confirm how can I tell if let say a tab is protected?
Jppinto,
thats exactly what I am thinking but I am not realy an excel guru so not sure if its protected but the idea is that if I can see or anything without a pass then its not protected now I can tell exactly where the records have been written but sure how to check if its protected
Idea is that it could just be a preventive active
Can you please confirm how can I tell if let say a tab is protected?
In short, a worksheet is protected if any of the worksheet properties ProtectContents, ProtectDrawingObjects, or ProtectScenarios are True. Typically, the ProtectContents property is the most commonly used as it defines whether or not locked cells are protected. From the Excel application user interface a worksheet cannot be protected unless the Protect Contents option is set on.
There are five protection related properties and objects in a worksheet object:
ProtectContents - True if locked cell contents (values and formulas) are protected, False otherwise. When a locked cell is protected it cannot be edited or changed with any commands. Cells are locked and unlocked from the Protection tab on the Format Cells dialog. The easiest method for determining if a worksheet is locked is to check the worksheet's ProtectContents property - if True then the worksheet is locked or protected.
ProtectDrawingObjects - True if all locked drawing objects are protected, False otherwise. When a locked drawing object is protected it cannot be edited or changed with any commands. Drawing objects are locked and unlocked from the Protection tab on the Format AutoShape and Format Picture dialogs.
ProtectScenarios - True if all locked scenarios are protected, False otherwise. When a locked scenario is protected it cannot be edited or changed with any commands. Scenarios are locked and unlocked by setting the "Prevent changes" checkbox on the Edit Scenario dialog accessable from the Scenario Manager dialog.
ProtectionMode - True if user interface only protection is enabled, False otherwise. This setting does not affect the behavior of the worksheet, it only affects whether or not VBA code is affected by the various protection modes. When True VBA is able to make modifications to any protected objects as if they were not protected.
Protection - A set of properties defining what other worksheet features are protected (not allowed). These are AllowDeletingColumns, AllowDeletingRows, AllowEditRanges, AllowFiltering, AllowFormattingCells, AllowFormattingColumns, AllowFormattingRows, AllowInsertingColumns, AllowInsertingHyperlinks, AllowInsertingRows, AllowSorting, and AllowUsingPivotTables. AllowEditRanges is a collection of AllowEditRange objects that contain various other properties describing who can edit what ranges. Note that all properties in the Protection object are only available in Excel 2002 and later versions.
Additional Notes
Executing the Unprotect command sets the ProtectContents, ProtectDrawingObjects, and ProtectScenarios to False, it does not set the ProtectionMode to False. When a subsequent Protect command is executed without specifying the UserInterfaceOnly parameter the ProtectionMode property is again left unchanged. The net result of this behavior is that the worksheet can be unprotected for the user and then reprotected again without the need to restore the ProtectionMode property. However, the ProtectionMode property is not saved with the workbook and is always reset to False when a workbook is opened. Therefore it must be maintained when a workbook is opened if VBA code relies on it being set to True.
While it is not possible to protect only drawing objects and/or scenarios while not protecting cells using the application user interface, it is possible using VBA:
TargetWorksheet.Protect Contents:=False, DrawingObjects:=True, Scenarios:=False
The Excel help states that the default value for the Protect command's Contents parameter is True - this is not true when setting either the DrawingObjects or Scenarios to False. When doing so and not specifying the Contents parameter, the ProtectContents property is set to False.
The ProtectionMode property that defines whether or not VBA has unrestricted access to the worksheet can only be set by using the Protect method's UserInterfaceOnly parameter. It has to be set everytime a workbook is opened as the property is not saved with the workbook. To set the property without affecting the other protection settings the Protect method must be executed by specifying all parameters as illustrated in the two examples below. This technique works because the Protect method is effective even if the worksheet is already protected.
In Excel 2002 and later use:
TargetSheet.Protect _
DrawingObjects:=TargetShee t.ProtectD rawingObje cts, _
Contents:=TargetSheet.Prot ectContent s, _
Scenarios:=TargetSheet.Pro tectScenar ios, _
UserInterfaceOnly:=True, _
AllowFormattingCells:=Targ etSheet.Pr otection.A llowFormat tingCells, _
AllowFormattingColumns:=Ta rgetSheet. Protection .AllowForm attingColu mns, _
AllowFormattingRows:=Targe tSheet.Pro tection.Al lowFormatt ingRows, _
AllowInsertingColumns:=Tar getSheet.P rotection. AllowInser tingColumn s, _
AllowInsertingRows:=Target Sheet.Prot ection.All owInsertin gRows, _
AllowInsertingHyperlinks:= TargetShee t.Protecti on.AllowIn sertingHyp erlinks, _
AllowDeletingColumns:=Targ etSheet.Pr otection.A llowDeleti ngColumns, _
AllowDeletingRows:=TargetS heet.Prote ction.Allo wDeletingR ows, _
AllowSorting:=TargetSheet. Protection .AllowSort ing, _
AllowFiltering:=TargetShee t.Protecti on.AllowFi ltering, _
AllowUsingPivotTables:=Tar getSheet.P rotection. AllowUsing PivotTable s
In Excel 2000 and earlier use:
TargetSheet.Protect _
DrawingObjects:=TargetShee t.ProtectD rawingObje cts, _
Contents:=TargetSheet.Prot ectContent s, _
Scenarios:=TargetSheet.Pro tectScenar ios, _
UserInterfaceOnly:=True
Note that while Excel does not reset the properties in the Protection object when unprotecting a worksheet, it does reset the three main properties ProtectContents, ProtectDrawingObjects, and ProtectScenarios which means that if the worksheet is ever unprotected using the Unprotect method, those three properties must be saved before hand and used when the worksheet is again protected.
Kevin
There are five protection related properties and objects in a worksheet object:
ProtectContents - True if locked cell contents (values and formulas) are protected, False otherwise. When a locked cell is protected it cannot be edited or changed with any commands. Cells are locked and unlocked from the Protection tab on the Format Cells dialog. The easiest method for determining if a worksheet is locked is to check the worksheet's ProtectContents property - if True then the worksheet is locked or protected.
ProtectDrawingObjects - True if all locked drawing objects are protected, False otherwise. When a locked drawing object is protected it cannot be edited or changed with any commands. Drawing objects are locked and unlocked from the Protection tab on the Format AutoShape and Format Picture dialogs.
ProtectScenarios - True if all locked scenarios are protected, False otherwise. When a locked scenario is protected it cannot be edited or changed with any commands. Scenarios are locked and unlocked by setting the "Prevent changes" checkbox on the Edit Scenario dialog accessable from the Scenario Manager dialog.
ProtectionMode - True if user interface only protection is enabled, False otherwise. This setting does not affect the behavior of the worksheet, it only affects whether or not VBA code is affected by the various protection modes. When True VBA is able to make modifications to any protected objects as if they were not protected.
Protection - A set of properties defining what other worksheet features are protected (not allowed). These are AllowDeletingColumns, AllowDeletingRows, AllowEditRanges, AllowFiltering, AllowFormattingCells, AllowFormattingColumns, AllowFormattingRows, AllowInsertingColumns, AllowInsertingHyperlinks, AllowInsertingRows, AllowSorting, and AllowUsingPivotTables. AllowEditRanges is a collection of AllowEditRange objects that contain various other properties describing who can edit what ranges. Note that all properties in the Protection object are only available in Excel 2002 and later versions.
Additional Notes
Executing the Unprotect command sets the ProtectContents, ProtectDrawingObjects, and ProtectScenarios to False, it does not set the ProtectionMode to False. When a subsequent Protect command is executed without specifying the UserInterfaceOnly parameter the ProtectionMode property is again left unchanged. The net result of this behavior is that the worksheet can be unprotected for the user and then reprotected again without the need to restore the ProtectionMode property. However, the ProtectionMode property is not saved with the workbook and is always reset to False when a workbook is opened. Therefore it must be maintained when a workbook is opened if VBA code relies on it being set to True.
While it is not possible to protect only drawing objects and/or scenarios while not protecting cells using the application user interface, it is possible using VBA:
TargetWorksheet.Protect Contents:=False, DrawingObjects:=True, Scenarios:=False
The Excel help states that the default value for the Protect command's Contents parameter is True - this is not true when setting either the DrawingObjects or Scenarios to False. When doing so and not specifying the Contents parameter, the ProtectContents property is set to False.
The ProtectionMode property that defines whether or not VBA has unrestricted access to the worksheet can only be set by using the Protect method's UserInterfaceOnly parameter. It has to be set everytime a workbook is opened as the property is not saved with the workbook. To set the property without affecting the other protection settings the Protect method must be executed by specifying all parameters as illustrated in the two examples below. This technique works because the Protect method is effective even if the worksheet is already protected.
In Excel 2002 and later use:
TargetSheet.Protect _
DrawingObjects:=TargetShee
Contents:=TargetSheet.Prot
Scenarios:=TargetSheet.Pro
UserInterfaceOnly:=True, _
AllowFormattingCells:=Targ
AllowFormattingColumns:=Ta
AllowFormattingRows:=Targe
AllowInsertingColumns:=Tar
AllowInsertingRows:=Target
AllowInsertingHyperlinks:=
AllowDeletingColumns:=Targ
AllowDeletingRows:=TargetS
AllowSorting:=TargetSheet.
AllowFiltering:=TargetShee
AllowUsingPivotTables:=Tar
In Excel 2000 and earlier use:
TargetSheet.Protect _
DrawingObjects:=TargetShee
Contents:=TargetSheet.Prot
Scenarios:=TargetSheet.Pro
UserInterfaceOnly:=True
Note that while Excel does not reset the properties in the Protection object when unprotecting a worksheet, it does reset the three main properties ProtectContents, ProtectDrawingObjects, and ProtectScenarios which means that if the worksheet is ever unprotected using the Unprotect method, those three properties must be saved before hand and used when the worksheet is again protected.
Kevin
"In short"? :)
I'm not in a short mood today.
I've noticed. There is such a thing as too much information, you know. ;)
Yep. Been there a few times. Sometimes I go long, sometimes I go short.
ASKER
I am not sure of I am write but problem is that I am unable to find any VBA code in the sheet which protects it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for help guys.
Kevin,
I think rorya was right. It actually was a little too much for me as I was looking for a quick fix. :)
but in the end you were right. I could just unprotect the sheet from UI and VBA code is just doing that to be able to do work on the sheet.
Thanks again
Kevin,
I think rorya was right. It actually was a little too much for me as I was looking for a quick fix. :)
but in the end you were right. I could just unprotect the sheet from UI and VBA code is just doing that to be able to do work on the sheet.
Thanks again
To protect a worksheet, choose the menu command Tools->Protection->Protect
To unprotect a worksheet, choose the menu command Tools->Protection->Unprote
To prevent the user from viewing formulas while the worksheet is protected, before protecting it select the cells for which the formulas need to be hidden, choose the menu command Format->Cells (in Excel 2007 and later navigate to the Home tab and select Format Cells from the Format menu in the Cells group or press ALT, O, E), navigate to the Protection tab, and check on the Locked and Hidden check boxes.
Kevin