+ Reply to Thread
Results 1 to 8 of 8

Locking cells except for one colour

Hybrid View

  1. #1
    Registered User
    Join Date
    08-17-2016
    Location
    Australia
    MS-Off Ver
    Office 365 Business
    Posts
    90

    Locking cells except for one colour

    I've found various threads on this, but not one that seems to work properly.

    I have a workbook with multiple sheets that I need to lock the contents of except for the cells that are light blue. Colour code is 20, I believe.

    Not sure if it makes a difference, but the cells either require data to be entered by the user or to choose an option from a dropdown box.

    Additionally Id like to hide all formulas so they're not visible to the end user.

    Much appreciate your help.


    Cheers,
    Matt

  2. #2
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Locking cells except for one colour

    Try this (untested - on my iPad!)
    Remember to lock all the worksheets first.
    Sub Unlock()
    For Each sht In ActiveWorkbook.Sheets
        Set rng = sht.UsedRange
            For Each cell In rng
                If cell.Interior.ColorIndex = 20 Then
                    cell.locked = False
                End If
            Next cell
    Next sht
    End Sub
    To find the current color index of a cell, select the cell and run this macro
    Sub Color
    MsgBox ActiveCell.Interior.ColorIndex
    End Sub
    Where are the formulas that you want to hide? Presumably not in the blue cells.
    Are you wanting all formulas in all sheets not visible - you only see formula if you select cells. So why not lock them out of selecting those cells?

  3. #3
    Registered User
    Join Date
    08-17-2016
    Location
    Australia
    MS-Off Ver
    Office 365 Business
    Posts
    90

    Re: Locking cells except for one colour

    The following issues;
    Sub Lock excel doesn't like, so I changed it to Unlock Blue. That was fine except now it comes up with a run time error for...
    cell.Locked = False

  4. #4
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Locking cells except for one colour

    Ooops
    Forgot to include line to unlock and re-lock each sheet
    Put your password inside the quote marks in both lines
    If there is no password then sht.Unprotect should also work

    Sub UnlockBlue()
    For Each sht In ActiveWorkbook.Sheets
        sht.Unprotect Password:=""
        Set Rng = sht.UsedRange
            For Each cell In Rng
                If cell.Interior.ColorIndex = 20 Then
                    cell.Locked = False
                End If
            Next cell
        sht.Protect Password:=""
    Next sht
    End Sub

  5. #5
    Registered User
    Join Date
    08-17-2016
    Location
    Australia
    MS-Off Ver
    Office 365 Business
    Posts
    90

    Re: Locking cells except for one colour

    Unfortunately its still coming up with errors

  6. #6
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Locking cells except for one colour

    I have tested it and it runs on my PC - so it must be something minor

    Run this and look at the Immediate Window to see if it is failing from the first sheet or whether it's a particular sheet that is causing the problem.

    To see the immediate window in VBA , Click on View, then "Immediate Window"

    Sub UnlockBlue()
    For Each sht In ActiveWorkbook.Sheets
        Debug.Print sht.Name
        sht.Unprotect Password:=""
        Set Rng = sht.UsedRange
            Debug.Print sht.Name & " " & Rng.Address(0, 0)
            For Each cell In Rng
                Debug.Print cell.Address(0, 0)
                If cell.Interior.ColorIndex = 20 Then
                    cell.Locked = False
                End If
            Next cell
        sht.Protect Password:=""
    Next sht
    End Sub

  7. #7
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Locking cells except for one colour

    read previous post first
    Here is my file for you to see the macro working
    I have not set any passwords
    The only cells that can be amended are on sheet2
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Locking cells except for one colour

    Additionally Id like to hide all formulas so they're not visible to the end user
    For Each sht In ActiveWorkbook.Sheets
        sht.Unprotect Password:=""
        Set Rng = sht.UsedRange
            For Each cell In Rng
                cell.FormulaHidden = True
            Next cell
        sht.Protect Password:=""
    Next sht
    Last edited by kev_; 12-01-2016 at 03:47 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Search cells based on student name, cell background colour and return a tally for colour
    By drof_06 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-26-2016, 04:31 AM
  2. [SOLVED] Locking Cells without locking worksheet
    By navialivad in forum Excel General
    Replies: 5
    Last Post: 11-24-2014, 12:44 PM
  3. Locking specific cells eventually changing to locking all cells
    By davidingilbert in forum Excel General
    Replies: 1
    Last Post: 03-19-2013, 06:50 PM
  4. Macro that clears contents of all cells of certain colour (colour 35) in all sheets
    By Hanskubansku in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-25-2012, 07:33 AM
  5. Locking Cell Fill Colour
    By claudiomonteverdi in forum Excel General
    Replies: 4
    Last Post: 03-30-2012, 02:18 AM
  6. Help with macro, automatical locking cells when excel is closed but only locking 1 sh
    By snoopy1461 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-21-2011, 11:29 AM
  7. Replies: 1
    Last Post: 07-21-2006, 11:05 AM

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