当行的 A 列对所有行都是 Yes 时锁定行中的选定单元格

Locking selected cells in row when A column of the row is Yes for all rows

我有一个电子表格,其中 A 列提出 Yes/No 问题,我需要电子表格锁定该行的单元格列 C、D、E 和 J。我需要在每一行上执行此操作。

所以每一行都以相同的 Yes/No 问题开头,然后根据对 Yes/No 的回答,一些单元格被锁定或保持解锁状态。

我一直在使用我在网上找到的一些示例代码,但它只适用于特定的行,我不确定如何将它应用于所有行。我找到的示例代码是:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("A*") = "Yes" Then
        Range("B1:B4").Locked = False
    ElseIf Range("A1") = "Refusing" Then
        Range("B1:B4").Locked = True
    End If
End Sub

我想知道如何创建通配符以将此代码应用于范围,以及如何锁定单元格 C*:E*、J* 等范围,我不是确定如何同时包含一个范围和另一个不在同一直接范围内的单元格。

试试下面的代码(未经测试)。正如@Comintern 提到的,Target 是已更改的范围。请注意,范围可以是单个单元格。使用 .Row 属性 returns 更改单元格的行。

Cells 允许您将行和列定义为两个单独的值。并且 Range 可以与 Cells 结合使用以获得单元格范围。

Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo esub   'If an error occurs reprotect the sheet
Me.Unprotect   'Remove sheet protection to avoid runtime error

If Target.Column=1 and Target.Value="Yes" Then
    Range(Cells(Target.Row,"C"),Cells(Target.Row,"E")).Locked=False
    Cells(Target.Row,"J").Locked=False

ElseIf Target.Column=1 and Target.Value="Refusing" Then
    Range(Cells(Target.Row,"C"),Cells(Target.Row,"E")).Locked=True
    Cells(Target.Row,"J").Locked=True
End If

esub:
Me.Protect    'Reprotect sheet
End Sub