+ Reply to Thread
Results 1 to 4 of 4

Locking Sheet based on Cell Value for a Macro

Hybrid View

  1. #1
    Registered User
    Join Date
    02-20-2012
    Location
    Austin
    MS-Off Ver
    Excel 2010
    Posts
    28

    Locking Sheet based on Cell Value for a Macro

    I am using a checkbox (forms not activeX). The checkbox is linked to cell A1, so A1 is True when the checkbox is checked and false when the checkbox is not checked.

    I want the sheet to be locked/protected when A1 is TRUE and when A1 is false have the sheet unlocked/unprotected.

    Any suggestions on how to achieve this?

  2. #2
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Locking Sheet based on Cell Value for a Macro

    First, you do not link this checkbox to a cell because when the sheet will be protected, you'll get an error because you are trying to write to cell A1. So the macro will make the link by programming. See macro below. I attatch a workbook as an working example.

    Sub CheckBox1_Click()
    If ActiveSheet.CheckBoxes("Check Box 1") = xlOn Then
      ActiveSheet.Range("a1") = True
      ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    Else
      ActiveSheet.Unprotect
      ActiveSheet.Range("a1") = False
    End If
    
    End Sub
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    02-20-2012
    Location
    Austin
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Locking Sheet based on Cell Value for a Macro

    If I want the entire sheet protected at all times except for lets say cells A3 to A8. So if the checkbox is checked no cells in the entire sheet can be edited. If the checkbox is unchecked only cells A3 to A8 can be edited. That is no data can be entered into the sheet except cells A3 to A8. Below is what I have this far from the previous program. I am new to VBA so not sure on the format but I can not get it to work.

    Sub CheckBox1_Click()
    If ActiveSheet.CheckBoxes("Check Box 1") = xlOn Then

    Range("A3:A8").Select
    Selection.Locked = True
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True

    Else

    Range("A3:A8").Select
    Selection.Locked = False
    Selection.Unprotect

    End If

    End Sub

  4. #4
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Locking Sheet based on Cell Value for a Macro

    First of all, you'll have to review forum rules because your last post do no comply with rule #3 asking you to use code tags to enclosed your codes.

    Now, find below the answer to your question: In fact, you want the sheet to be protected all the time except for some cells that you want to unlock when the checkbox is checked. So here is the code to do that:
    Sub CheckBox1_Click()
    If ActiveSheet.CheckBoxes("Check Box 1") = xlOn Then
      ActiveSheet.Unprotect
      ActiveSheet.Range("a1") = True
      Range("A3:A8").Select
      Selection.Locked = True
      Selection.FormulaHidden = False
      ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
      Range("A1").Select
    Else
      ActiveSheet.Unprotect
      ActiveSheet.Range("a1") = False
      Range("A3:A8").Select
      Selection.Locked = False
      Selection.FormulaHidden = False
      ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
      Range("A1").Select
    End If
    End Sub
    In the attached file, you'll find a working workbook.
    regards
    Pierre
    Attached Files Attached Files

+ 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