以前锁定的单元格通过 Target.Row 值解锁

发布于 2025-01-11 12:55:12 字数 723 浏览 0 评论 0原文

我正在尝试编写代码来锁定相对于目标行的单元格。 例如,如果我们将“优先级”列选择为“是”,则应锁定“Y1 列”。我有能力做到这一点。当我在下一行工作时,问题就出现了。如果我们在下一行中选择“优先级”列为“是”,则单元格 c3 被锁定,但单元格 C2 被解锁,如何使单元格 C2 保持锁定状态

到目前为止我的代码:

Private Sub Worksheet_Change(ByVal Target As Range)

If target.column = 1 then
  If sheet1.range("A" & Target.Row).value = "Yes" then
    
sheet1.range("C" & target.row).locked = true
  activesheet.protect password:="pass"
  else
    sheet1.range("C" & target.row).locked = false
  End if

如果我们在第 2 行上工作,则单元格 C2 被锁定,但如果我们处理第 3 行,然后单元格 C3 被锁定,但 C2 被解锁。我们怎样才能保持C2处于锁定状态

谢谢在此处输入图像描述

I am trying to write a code to Lock a cell with respect to target row.
In example, If we select Priority column as Yes then Column Y1 should be locked. I am able to do that. The problem comes when I work on next row. If we select Priority column as Yes in the next row, then cell c3 gets locked but cell C2 gets unlocked, how to keep the cell C2 as locked

My code so far :

Private Sub Worksheet_Change(ByVal Target As Range)

If target.column = 1 then
  If sheet1.range("A" & Target.Row).value = "Yes" then
    
sheet1.range("C" & target.row).locked = true
  activesheet.protect password:="pass"
  else
    sheet1.range("C" & target.row).locked = false
  End if

If we work on Row 2 then cell C2 gets locked, but if we work on Row 3 then cell C3 gets locked but C2 gets unlocked. How can we keep C2 as locked

Thanksenter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺渁∝ 2025-01-18 12:55:12

工作表更改:锁定单元格

  • 如果您不是 OP,最好在将代码复制到工作表模块(例如 Sheet1)之前解锁所有单元格。
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    
    Const FirstCellAddress As String = "A2"
    Const LockCellColumn As String = "C"
    
    Dim irg As Range
    With Range(FirstCellAddress)
        Set irg = Intersect(.Resize(Me.Rows.Count - .Row + 1), Target)
    End With
    If irg Is Nothing Then Exit Sub
    
    Application.EnableEvents = False
    Me.Unprotect Password:="pass"
    
    Dim iCell As Range
    Dim DoIt As Boolean
    Dim LockIT As Boolean
    
    For Each iCell In irg.Cells
        
        Select Case CStr(iCell.Value)
        Case "Yes"
            DoIt = True: LockIT = True
        Case "No"
            DoIt = True: LockIT = False
        Case Else ' do nothing
            DoIt = False
        End Select
        
        If DoIt Then
            iCell.EntireRow.Columns(LockCellColumn).Locked = LockIT
        End If
    
    Next iCell

    Me.Protect Password:="pass"
    Application.EnableEvents = True

End Sub

A Worksheet Change: Locking Cells

  • If you're not OP, best unlock all cells before copying the code to the sheet module e.g. Sheet1.
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    
    Const FirstCellAddress As String = "A2"
    Const LockCellColumn As String = "C"
    
    Dim irg As Range
    With Range(FirstCellAddress)
        Set irg = Intersect(.Resize(Me.Rows.Count - .Row + 1), Target)
    End With
    If irg Is Nothing Then Exit Sub
    
    Application.EnableEvents = False
    Me.Unprotect Password:="pass"
    
    Dim iCell As Range
    Dim DoIt As Boolean
    Dim LockIT As Boolean
    
    For Each iCell In irg.Cells
        
        Select Case CStr(iCell.Value)
        Case "Yes"
            DoIt = True: LockIT = True
        Case "No"
            DoIt = True: LockIT = False
        Case Else ' do nothing
            DoIt = False
        End Select
        
        If DoIt Then
            iCell.EntireRow.Columns(LockCellColumn).Locked = LockIT
        End If
    
    Next iCell

    Me.Protect Password:="pass"
    Application.EnableEvents = True

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文