【问题标题】:Range of cells in a row being editable/not editable based on an adjacent cell value基于相邻单元格值可编辑/不可编辑的行中的单元格范围
【发布时间】:2020-12-13 09:05:38
【问题描述】:

我需要什么:

  • 当文本 Yes 输入到例如 A2 时,我希望 B2:E2 变得无法编辑,但 A3:E7 范围仍然可以编辑。
  • 当文本 Yes 输入到例如 A3 时,我希望 B3:E3 变为不可编辑,此外 B2:E2(上一行)不可编辑。
  • 上述功能将在工作表上持续大约 100 行。
  • 当从单元格(例如 A2)中删除文本是时,只有 B2:E2 再次变为可编辑。当在单元格中输入“是”时,它们将无法再次编辑。
  • 如果 A 列中的单元格为空白,则该行对应的 B:E 列是可编辑的。

附图中的当前状态: 单元格 A2:E7 已解锁,工作表中的所有其他单元格保持锁定状态 表格的其余部分在没有密码的情况下受到保护/仅选择未锁定的单元格(但如果需要,可以包含密码)

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    下面的代码必须在 VBA 项目属性下的 Worksheet 对象中输入。我已经测试了代码,它似乎正在工作。如果出现任何问题,请报告。如果您想扩展代码的工作范围,只需将 currSheet.Range("A2") 编辑为 currSheet.Range("A100") 等即可。

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim currSheet As Worksheet
    Set currSheet = ThisWorkbook.ActiveSheet
    
    'declare source range - can be a cell, a range of cells, entire column, entirerow etc.
    Dim sourceCell As Range
    Set sourceCell = currSheet.Range("A2")
    'declare range that will be locked/unlocked
    Dim tarRange As Range
    Set tarRange = currSheet.Range("B2:E2")
    
    Application.ScreenUpdating = False
    currSheet.Unprotect
    If Target = sourceCell Then
        set_cellLockStatus sourceCell, "Yes", tarRange, True
    End If
    Application.ScreenUpdating = True
    currSheet.Protect
    End Sub
    
    Public Sub set_cellLockStatus(source_rng As Range, sourceValueCondition As Variant, target_rng As Range, lockCell As Boolean)
    'take source range, check if source condition is met - if yes, lock or unlock target range
    source_rng.Locked = False
        With source_rng
            If source_rng.Value2 = sourceValueCondition Then
                target_rng.Locked = lockCell
                target_rng.Interior.ColorIndex = 8
            Else:
                target_rng.Locked = Not lockCell
                target_rng.Interior.ColorIndex = 0
            End If
        End With
    End Sub
    

    【讨论】:

    • 感谢您抽出宝贵时间提供帮助。我会试试这个
    【解决方案2】:

    另一种解决方案。工作表中的代码 - 更改事件:

    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Target.Column = 1 Then                   ' column A changed
            ThisRow = Target.Row                    'get row number
            If Target.Value = "Yes" Then
                ThisWorkbook.ActiveSheet.Unprotect  'unprotect sheet
                Range("B" & ThisRow).Locked = True
                Range("C" & ThisRow).Locked = True
                Range("D" & ThisRow).Locked = True
                Range("E" & ThisRow).Locked = True
                ThisWorkbook.ActiveSheet.Protect    'protect sheeet
            Else
                ThisWorkbook.ActiveSheet.Unprotect
                Range("B" & ThisRow).Locked = False
                Range("C" & ThisRow).Locked = False
                Range("D" & ThisRow).Locked = False
                Range("E" & ThisRow).Locked = False
                ThisWorkbook.ActiveSheet.Protect
            End If
        End If
        
    End Sub
    
    
    

    【讨论】:

    • 感谢您抽出宝贵时间提供帮助。我会试试这个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    • 2021-05-20
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    相关资源
    最近更新 更多