+ Reply to Thread
Results 1 to 9 of 9

Change cell fill color when unprotected cell selected -

Hybrid View

  1. #1
    Registered User
    Join Date
    11-24-2012
    Location
    Stratford-upon-Avon
    MS-Off Ver
    Excel 2007
    Posts
    89

    Change cell fill color when unprotected cell selected -

    I'm struggling to find some code that changes the color (to light grey) of an unprotected cell in my sheet when its selected. When an unprotected cell is selected I want it to change color. The cell is then filled with a value and its color to return to original color when next unprotected cell selected.

  2. #2
    Forum Contributor
    Join Date
    01-03-2013
    Location
    Aberdeen, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    163

    Re: Change cell fill color when unprotected cell selected -

    I don't know if this code meets your requirements, but maybe you find it a bit helpful:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    ActiveSheet.Protect UserInterfaceOnly:=True
    Cells.Interior.ColorIndex = xlNone
    If ActiveCell.Locked = False Then ActiveCell.Interior.Color = RGB(221, 221, 221)
    End Sub

  3. #3
    Valued Forum Contributor
    Join Date
    06-19-2010
    Location
    Holywell, N Wales, UK
    MS-Off Ver
    Excel 2013
    Posts
    470

    Re: Change cell fill color when unprotected cell selected -

    With acknowledgement to Sbarro79
    This variation will preserve the cell colors of existing protected cells
    Note that it uses a helper cell ("XFD1") so this can't be used for other things

    Also this code must be placed in the appropriate Sheet module.

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim rng As String
    ActiveSheet.Protect UserInterfaceOnly:=True
        If Target.Locked = False Then
            rng = Range("XFD1").Value
            Range(rng).Interior.ColorIndex = xlNone
            Target.Interior.ColorIndex = 15
            Range("XFD1").Value = Target.Address
        End If
    End Sub
    If this was helpful then please click the small star icon at the bottom left of my post. A little appreciation goes a long way.

  4. #4
    Forum Contributor
    Join Date
    01-03-2013
    Location
    Aberdeen, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    163

    Re: Change cell fill color when unprotected cell selected -

    Point for you barryleajo - I didn't think that there may be some protected cells already filled with color (but I am still a beginner in VBA and Excel).
    Anyway here is my version of code that preserves colors in protected cells:

    Private prevcell As Range
    to be added above all procedures in Sheet module (global variable)


    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    ActiveSheet.Protect UserInterfaceOnly:=True
    
    If prevcell Is Nothing Then Set prevcell = Target
    If Not prevcell Is Nothing And prevcell.Locked = False Then prevcell.Interior.ColorIndex = xlNone
    If Target.Locked = False Then Target.Interior.Color = RGB(221, 221, 221)
    Set prevcell = Target
    End Sub
    I also added that line:

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If ActiveCell.Locked = False Then ActiveCell.Interior.ColorIndex = xlNone
    End Sub
    to avoid saving Workbook with unprotected cell filled with color

    I don't know if the code is written properly, but it works for me

  5. #5
    Registered User
    Join Date
    11-24-2012
    Location
    Stratford-upon-Avon
    MS-Off Ver
    Excel 2007
    Posts
    89

    Re: Change cell fill color when unprotected cell selected -

    Hi Sbarro79. Your first code worked great but I lost the fill colour in all my protected cells. Could you please explain where I put 'Private prevcell As Range' within the code you have given. Much appreciated.

  6. #6
    Forum Contributor
    Join Date
    01-03-2013
    Location
    Aberdeen, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    163

    Re: Change cell fill color when unprotected cell selected -

    Hi Stratfordoaks, sorry for late reply - work
    For the code line you asked: just open your workbook, open VBA editor (ALT+F11), in the top left panel select your worksheet (double click) and the code should be put in the first or second line (if the first line is Option Explicit)

    I also changed the code:
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If ActiveCell.Locked = False Then ActiveCell.Interior.ColorIndex = xlNone
    End Sub
    to that code:

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Worksheets("x").Activate
    If ActiveCell.Locked = False Then ActiveCell.Interior.ColorIndex = xlNone
    End Sub
    because I experienced that previous code didn't work when I clicked save button before closing workbook. And now, I hope, it will be alright.
    Please remember to change "x" to worksheet name you work on.

    I hope I explained everything clearly enough.

  7. #7
    Registered User
    Join Date
    11-24-2012
    Location
    Stratford-upon-Avon
    MS-Off Ver
    Excel 2007
    Posts
    89

    Re: Change cell fill color when unprotected cell selected -

    Hi Sbarro79 - I did get it working yesterday but I have other macros that run from this sheet before it saves and closes. I think the code was protecting cells and stopped these macros working. Its very frustrating because the grey fill colur in selected cell made it far easier for the operator to see which cell they were in. I need your code to not work when my oter ones run. I dont know if thats possible? Ps could you put all your code together for me so that I am sure I have it all in order. Im not quite sure which bits you want me to use. Many thanks for your time and patience.

  8. #8
    Forum Contributor
    Join Date
    01-03-2013
    Location
    Aberdeen, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    163

    Re: Change cell fill color when unprotected cell selected -

    Sorry to hear that there is a problem with my code. I'm just guessing that your worksheet was already protected when you open the workbook and this didn't allow your macros to be executed. But as I wrote, I'm still a beginner in VBA and I need to learn more about it, and learn, and learn...
    In the attached file you will find code (slightly updated, if we can say like that) plus some silly simple macro (commandbutton_click and trial procedure) that works when sheet is protected (you don't need to add it to your worksheet). Please check, if the code works now as you wanted.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    11-24-2012
    Location
    Stratford-upon-Avon
    MS-Off Ver
    Excel 2007
    Posts
    89

    Re: Change cell fill color when unprotected cell selected -

    Thanks Sbarro79. I will have a dabble in the morning and let you know how it goes.

+ 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