Registrations are now open. Join us today!
There is still a lot of work to do on the wiki yet! More information about editing can be found here.
Already have an account?

Microsoft KB Archive/168842

From BetaArchive Wiki

Article ID: 168842

Article Last Modified on 11/23/2006



APPLIES TO

  • Microsoft Excel 97 Standard Edition



This article was previously published under Q168842


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 professionals 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 a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

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

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: kbbug kbdtacode kberrmsg KB168842