+ Reply to Thread
Results 1 to 4 of 4

Cannot set background color in unlocked cell

Hybrid View

  1. #1
    Registered User
    Join Date
    10-31-2011
    Location
    Near London, England
    MS-Off Ver
    Excel 2003
    Posts
    11

    Cannot set background color in unlocked cell

    I am using a code fragment like this to set the background color of a cell:

    Cells(x,y).Interior.ColorIndex = 6

    The cell is not locked, to let the user change its value.

    This works fine, until I protect the worksheet, with "allow all users to select unlocked cells". Then, when I run the program, I get the error:
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Runtime Error 1004
    Unable to set the ColorIndex property of the Interior Class
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Why is this an error (although the sheet is protected, the cell is unlocked), and how can I work around this?

    --
    Ian

  2. #2
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: Cannot set background color in unlocked cell

    Remember to put code tags around your code!

    Try unlocking at then locking your sheet before changing the cells color.

    Sub steffen()
    
    Sheets(1).Unprotect
       Cells(x,y).Interior.ColorIndex = 6
    Sheets(1).Protect
    End Sub
    Please take time to read the forum rules

  3. #3
    Registered User
    Join Date
    10-31-2011
    Location
    Near London, England
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Cannot set background color in unlocked cell

    Quote Originally Posted by Steffen Thomsen View Post
    Remember to put code tags around your code!
    As it was such a small fragment I thought I could sneak it in without you noticing .
    Quote Originally Posted by Steffen Thomsen View Post
    Try unlocking at then locking your sheet before changing the cells color.
    Thanks Steffen. Just what I need.

    --
    Ian

  4. #4
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: Cannot set background color in unlocked cell

    Not to be picky, but if your question was answered satisfactory, then remember to mark the thread as solved to

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1