+ Reply to Thread
Results 1 to 7 of 7

Use a function to turn cell protection on or off

  1. #1
    Registered User
    Join Date
    02-19-2014
    Location
    Seattle, WA USA
    MS-Off Ver
    Excel 2007
    Posts
    42

    Use a function to turn cell protection on or off

    Is there a function to turn the cell protection on or off? For example in the attached spreadsheet, data is entered in the yellow-colored, unprotected cells in the top table. Calculations are recorded in the orange, protected cells. As a lazy way to avoid having to repeat data entry in the second table, there is a check box that controls whether the values in the first table are copied to the second table. If the box is unchecked (and cell B23 is "False") the analyst will manually enter the new set of data in the second table. For this reason the cells must remain unprotected to allow data entry when the spreadsheet is protected. Is there a way/function to turn on the cell protection when the checkbox is checked (B23=TRUE) and turn off the cell protection when the box is unchecked (B23=False). I tried using the function CELL but that just informs you of the condition of the cell. It does not control the cell format.

  2. #2
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: Use a function to turn cell protection on or off

    Take a look at this. It kind works, other than throwing up a message each time you click the checkbox..
    vtentarelli-use-a-function-to-turn-cell-protection-on-or-off-11-20-2017-control-cell-protection.xlsm
    1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG
    You don't have to add Rep if I have helped you out (but it would be nice), but please mark the thread as SOLVED if your issue is resolved.

    Tom

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Use a function to turn cell protection on or off

    Hi,

    Just a semantic correction. Cells aren't protected they are either locked or not. A sheet is said to be protected or not. That said I understand what you're getting at.

    One way would be to use the Click Event of the Checkbox. Name the cells that you want to be toggled between Locked or not as say "MyRange". Then use this macro

    Please Login or Register  to view this content.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Registered User
    Join Date
    02-19-2014
    Location
    Seattle, WA USA
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Use a function to turn cell protection on or off

    I did not receive the results I expected with your macro, but it could be because I am not very familiar with macros. I named the sequence of cells, "MyRange" as you instructed. I then selected the Developer tab and clicked on the Design Mode icon. I placed my curser on the check box and right clicked to view the selections in the pop-up window. I selected "View Code" then pasted your macro in the open window with the heading CheckBox1/Click. I closed the window and ended the Design Mode session. I clicked the box and the entries in the top table appeared in the bottom table. However, I was able to edit those cells included in MyRange. Was there an error in my steps? Do I have to protect the worksheet for this macro to work? If I protect the worksheet and click the checkbox, I get a Visual Basic Error message: "Unable to set the Locked property of the Range class. If I select Debug the following line of code is highlighted in yellow: : Range("MyRange").Cells.Locked = True. I have a feeling I'm missing some critical step. Any ideas?

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Use a function to turn cell protection on or off

    Hi,

    Yes, I sort of assumed that you would be protecting the worksheet since that's the only way the locked statis of cells becomes operative. Think of worksheet prorection and cell locking like a field with an electric fence around it. The fence is useless unless yuo turn on the electricity.

    There was no need to use the macro recorder you could just have copied the code I gave you into the Click event of the control.

    Try adding the line

    Sheet1.Unprotect immediatel after the IF line and finish the macro with the Sheet1.Protect instruction

  6. #6
    Registered User
    Join Date
    02-19-2014
    Location
    Seattle, WA USA
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Use a function to turn cell protection on or off

    Once I corrected my typo, the code worked well. Another step was for me to figure out how to include two disjointed group of cells into the macro. I wasn't very successful with naming a range, so I just used the cell references instead. Once properly installed, I received no more error messages and got the results I wanted. Thanks again.

  7. #7
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Use a function to turn cell protection on or off

    Hi

    Just select all the cells you want to lock and then add the name in the name box.

+ 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] Write a cell function that will take the number 16 and turn it into 1+6=7
    By Hedy in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 12-30-2014, 04:12 PM
  2. [SOLVED] How do I turn on Protection UserInterFaceOnly on a shared workbook?
    By scottbass in forum Excel General
    Replies: 5
    Last Post: 01-12-2014, 06:52 PM
  3. turn this into a function?
    By bruizer31 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 07-09-2013, 02:23 PM
  4. Excel 2007 : Can I turn on/off cell function
    By robbcs3 in forum Excel General
    Replies: 2
    Last Post: 03-06-2011, 11:53 AM
  5. Modify macro to check and turn off sheet protection.
    By dcgrove in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-17-2009, 10:53 AM
  6. How to turn a spreadsheet into a function?
    By [email protected] in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-13-2005, 10:20 AM
  7. How do I use turn GETPIVOTDATA function off?
    By John Michl in forum Excel Formulas & Functions
    Replies: 18
    Last Post: 09-06-2005, 07:05 PM

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