XL97: Run-Time Error Using SpecialCells Method with Protection

Last reviewed: March 13, 1998
Article ID: Q168842
The information in this article applies to:
  • Microsoft Excel 97 for Windows

SYMPTOMS

When 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.

CAUSE

This problem occurs if the following conditions are true:

  • You are running a Visual Basic for Applications macro or procedure that selects cells by using the SpecialCells method.

        -and-
    
  • The contents of the active worksheet are currently protected.

WORKAROUND

Microsoft 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 Statement

Some 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 Sub

The following example simulates the xlCellTypeNotes constant of the SpecialCells method by pressing the CTRL+SHIFT+O key combination:

   Sub Test2()
      Application.SendKeys "^+O"
   End Sub

The other constants (xlCellTypeConstants, xlCellTypeFormulas, xlCellTypeBlanks, and xlCellTypeVisible) cannot be simulated by using key combinations.

Method 2: Unprotect the Worksheet

If 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 Sub

NOTE: If the worksheet is protected by a password, you must supply the password as an argument for the Unprotect method.

STATUS

Microsoft 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.

REFERENCES

For more information about keyboard commands, click the Index tab in Microsoft Excel Help, type the following text

   keyboard shortcuts

and then double-click the selected text to go to the "Use shortcut keys in Microsoft Excel" topic.


Additional query words: XL97 goto
Keywords : kbcode kberrmsg xlvbainfo
Version : WINDOWS:97
Platform : WINDOWS
Issue type : kbbug
Solution Type : kbworkaround


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 13, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.