Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XL2000: Protect Method Password Is Not Applied to Worksheet


View products that this article applies to.

This article was previously published under Q213762

↑ Back to the top


Symptoms

When you use the Protect method in a Microsoft Visual Basic for Applications macro to apply password protection to a worksheet, Microsoft Excel 2000 does not apply the password to the worksheet.

↑ Back to the top


Cause

This problem occurs when all of the following conditions are true:
  • You use a second ActiveSheet.Protect command that specifies a password in the macro.

    -and-
  • You set all the arguments (Contents, Scenarios, and DrawingObjects) for the second ActiveSheet.Protect command to True.
NOTE: Microsoft Excel may not apply password protection, depending on the values of the arguments for the Protection statement. In some cases, Microsoft Excel applies worksheet protection but does not apply a password even if you specify a password in the macro.

↑ Back to the top


Workaround

To work around this problem, use either of the following methods.

Method 1

Use the ActiveSheet.Protect command only once and supply all the arguments, including the password. For example, use the following macro:
Sub ProtectSheet()

   ActiveSheet.Protect password:="pw", Contents:=True, _
      Scenarios:=False, DrawingObjects:=True, UserInterfaceOnly:=True

End Sub
				

Method 2

Set one of the Protect method arguments to False. To do this, use either of the following methods:
  • If the macro is recorded, click to clear one of the following check boxes: Contents, Objects, or Scenarios.

    -or-
  • If the macro is manually typed, use the following syntax
    Sub ProtectSheet()
    
       ActiveSheet.Protect password:="pw", Contents:=True, _
          Scenarios:=False, DrawingObjects:=True, _
          UserInterfaceOnly:=True
    
    End Sub
    						
    to set Contents, Scenarios, or DrawingObjects to False.

↑ Back to the top


Status

Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

↑ Back to the top


More information

Microsoft Excel 2000 allows you to add an additional layer of protection by applying password protection to a previously protected sheet. You can add this protection programmatically.

In some earlier versions of Microsoft Excel, using the ActiveSheet.Protect command more than once has no effect. In Excel 2000, using the command a second time with a password applies password-level security to a previously protected sheet as long as the arguments for Contents, Scenarios, and DrawingObjects are not all set to True. When all the arguments are set to True, Excel protects the active sheet but does not apply password-level protection. The value of the UserInterfaceOnly argument has no effect on this behavior.

The following sample code demonstrates the problem:
Sub Test()

   ActiveSheet.Protect
   ActiveSheet.Protect password:="pw", contents:=True, _
      Scenarios:=True, DrawingObjects:=True, UserInterfaceOnly:=False

End Sub
				

↑ Back to the top


References

For more information about protecting information in Microsoft Visual Basic for Applications programming, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type ways to protect sensitive information in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

↑ Back to the top


Keywords: KB213762, kbprogramming, kbpending, kbdtacode, kbbug

↑ Back to the top

Article Info
Article ID : 213762
Revision : 6
Created on : 10/11/2006
Published on : 10/11/2006
Exists online : False
Views : 249