简体   繁体   English

使用条件格式锁定/解锁单元格

[英]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 这是一篇有助于向我解释的文章: http : //database.ittoolbox.com/groups/technical-functional/excel-l/how-to-lockunlock-an-excel-cell-based-on-the-contents-另一个单元的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. 若要执行您描述的操作,您将需要创建一个事件过程,只要工作表的内容发生更改,Excel就会调用该过程。 Start by opening the Visual Basic window (press Alt+F11). 首先打开Visual Basic窗口(按Alt + F11)。 You should see s tree view in a pane at the top left; 您应该在左上方的窗格中看到s树视图; 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. Excel将自动添加SelectionChange事件过程的“框架”。 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. Excel还将右侧下拉列表中的选择更改为SelectionChange。 Open that list and select Change instead. 打开该列表,然后选择“更改”。 Excel will add a second event procedure, which should look like this: Excel将添加第二个事件过程,该过程应如下所示:

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. 您想要向Worksheet_Change过程添加代码以检查G2单元格的内容,并更改G3:G66范围的状态。 This requires one IF statement, and a couple of assignment statements: 这需要一个IF语句和几个赋值语句:

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). IF语句测试单元格G2的当前内容(Cells()方法采用行-2-和列号-7-标识单元格;列G为第七列)。 The two assignment statements change the Locked property of the range; 这两个赋值语句更改范围的Locked属性。 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: 为了避免执行代码过于频繁,可以使用传递给事件过程的Target参数:

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. 此条件语句使用Intersect()函数来确定单元格G2(ActiveSheet.Cells(2,7))是否包含在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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