XL97: Run-Time Error Using SpecialCells Method with ProtectionLast reviewed: March 13, 1998Article ID: Q168842 |
The information in this article applies to:
SYMPTOMSWhen you attempt to use the SpecialCells Method in a Microsoft Visual Basic for Applications macro or procedure in Microsoft Excel, you may receive the following error message:
Run-time error '1004': You cannot use this command on a protected sheet. To unprotect the sheet, use the Unprotect Sheet command (Tools menu, Protection submenu). You may be prompted for a password. CAUSEThis problem occurs if the following conditions are true:
WORKAROUNDMicrosoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/refguide/default.asp Method 1: Use the SendKeys StatementSome of the SpecialCells constants have keyboard equivalents that can be selected by pressing a specific key combination. For example, to select the last cell in the used range using the SpecialCells method, the xlCellTypeLastCell constant is used. This operation can be simulated on the worksheet by pressing the CTRL+END key combination. Using the SendKeys statement, a macro can simulate pressing this key combination. The following is a macro that provides an example of pressing the CTRL+END key combination and will operate when the worksheet contents are protected:
Sub Test() Application.SendKeys "^{END}" End SubThe following example simulates the xlCellTypeNotes constant of the SpecialCells method by pressing the CTRL+SHIFT+O key combination:
Sub Test2() Application.SendKeys "^+O" End SubThe other constants (xlCellTypeConstants, xlCellTypeFormulas, xlCellTypeBlanks, and xlCellTypeVisible) cannot be simulated by using key combinations.
Method 2: Unprotect the WorksheetIf the SpecialCells method is used on a worksheet with the contents protected, the macro will produce the error listed in the "Symptoms" section of this article. To avoid this error, unprotect the sheet before using the SpecialCells method. Reapply the protection once the cells have been selected. This example unprotects the sheet, selects all blank cells in the used range, and then protects the sheet:
Sub Test3() ActiveSheet.Unprotect Selection.SpecialCells(xlCellTypeBlanks).Select ActiveSheet.Protect DrawingObjects:=True, Contents:=True, _ Scenarios:=True End SubNOTE: If the worksheet is protected by a password, you must supply the password as an argument for the Unprotect method.
STATUSMicrosoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.
REFERENCESFor more information about keyboard commands, click the Index tab in Microsoft Excel Help, type the following text
keyboard shortcutsand then double-click the selected text to go to the "Use shortcut keys in Microsoft Excel" topic.
|
Additional query words: XL97 goto
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |