简体   繁体   中英

Lock/Unlock a cell using conditional formatting

如何使用条件格式基于另一个单元格(A2)中的值(是/否)锁定/解锁单元格(A1)?

There's lots of resources online showing how to do this. Here's an article that helped explain it to me: http://database.ittoolbox.com/groups/technical-functional/excel-l/how-to-lockunlock-an-excel-cell-based-on-the-contents-of-another-cell-4625040

In case that link dies, here is the gist of it:

To do what you are describing, you will need to create an event procedure that Excel will call whenever the contents of the worksheet are changed. Start by opening the Visual Basic window (press Alt+F11). You should see s tree view in a pane at the top left; find the name of the worksheet in that view, and double-click the name. This will open the code module associated with that worksheet in the large pane on the right.

At the top of the large pane you will see two drop-down lists. Initially, the one on the left will display (General), while the one on the right will display (Declarations). Click the triangular arrowhead at the right end of the left list, and select Worksheet instead.

Excel will automatically add the "skeleton" of a SelectionChange event procedure. That's not what we want, but it won't hurt anything.

Excel will also change the selection in the right-hand drop-down list to SelectionChange. Open that list and select Change instead. Excel will add a second event procedure, which should look like this:

Private Sub Worksheet_Change(ByVal Target As Range)

End Sub

The blinking cursor will be positioned on the blank line of this skeleton.

You want to add code to the Worksheet_Change procedure to examine the contents of the G2 cell, and change the state of the G3:G66 range. This requires one IF statement, and a couple of assignment statements:

Private Sub Worksheet_Change(ByVal Target As Range)
    If ActiveSheet.Cells(2, 7).Text = "X" Then
        ActiveSheet.Range(Cells(3, 7), Cells(66, 7)).Locked = True
    Else
        ActiveSheet.Range(Cells(3, 7), Cells(66, 7)).Locked = False
    End If
End Sub

The IF statement tests the current contents of cell G2 (the Cells() method takes a row -- 2 -- and a column number -- 7 -- to identify the cell; column G is the seventh column). The two assignment statements change the Locked property of the range; one locks the cells, the other unlocks them.

Of course, if your code were more complicated, you would want to avoid running it every time anything on the worksheet changes. To avoid executing the code too often, you can use the Target parameter that is passed to the event procedure:

If Intersect(ActiveSheet.Cells(2, 7), Target) _
    Is Not Nothing Then

This conditional statement uses the Intersect() function to determine if cell G2 ( ActiveSheet.Cells(2, 7)) is included in Target.

Therefore, the complete event procedure would look like:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(ActiveSheet.Cells(2, 7), Target) Is Not Nothing Then
        If ActiveSheet.Cells(2, 7).Text = "X" Then
            ActiveSheet.Range(Cells(3, 7), Cells(66, 7)).Locked = True
        Else
            ActiveSheet.Range(Cells(3, 7), Cells(66, 7)).Locked = False
        End If
    End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM