【问题标题】:Unlock a specif Row Range based on the date根据日期解锁特定的行范围
【发布时间】:2017-07-19 22:03:19
【问题描述】:

我需要一些帮助来升级我的 VBA 代码。

我尝试找到一个可以根据当前日期解锁特定行的代码。问题是,我不希望解锁所有行的单元格,而只解锁一组特定范围。就像在“B”列中的当前日期一样,解锁的单元格将从(“D”到“K”); (“M”到“P”); (“R”到“S”)和(“U”到“V”)。

中间的单元格包含我不希望人们弄乱或错误更改的公式。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Range("B" & Selection.Row).Value <> Date Then
        ActiveSheet.Protect Password:="3827"
        MsgBox "Only today's date needs to be edited!", vbInformation, "REMINDER"
    ElseIf Range("B" & Selection.Row).Value = Date Then
        ActiveSheet.Unprotect Password:="3827"
        ActiveSheet.EnableSelection = xlNoRestrictions
    End If
End Sub

【问题讨论】:

  • 在保护工作表之前尝试类似“range.locked = False”的操作。保护工作表时,只会保护锁定的单元格。未锁定的单元格(即,locked = false)不会在受保护的工作表上受到保护。
  • 这可以让你看到多列范围Range("D:K, M:P, R:S, U:V").Select ...尽管使用这种方式:@Jarom 提到的Range("D:K, M:P, R:S, U:V").Locked = False
  • 对于那些列中的单行 ...intersect(rows(8),Range("D:K, M:P, R:S, U:V")).Selectintersect(rows(8),Range("D:K, M:P, R:S, U:V")).Locked = False ...不要忘记,.Select 仅用于调试,以便您可以看到结果范围。不要在生产代码中使用它,除非真的非常必要
  • Rows(8).Range("D1:K1, M1:P1, R1:S1, U1:V1")
  • @TimWilliams 我比 intersect 更喜欢这种形式

标签: vba excel date locking unlock


【解决方案1】:

为什么不更进一步呢?只让他们在激活工作表时选择那些列的今天日期所在的行!

Option Explicit

Private Const PWD As String = "3827"
Private Const UNLOCK_COLS As String = "D:K,M:P,R:S,U:V"

Private Sub Worksheet_Activate()
    Dim dToday As Date, oRng As Range, oItem As Variant
    dToday = Date
    With ActiveSheet
        .Unprotect Password:=PWD
        .Cells.Locked = True
        ' Look for row with today's date and unlock the row inside usedrange
        Set oRng = .Columns("B").Find(What:=dToday)
        If Not oRng Is Nothing Then
            For Each oItem In Split(UNLOCK_COLS, ",")
                Intersect(oRng.EntireRow, .Columns(oItem)).Locked = False
            Next
        End If
        .Protect Password:=PWD
        .EnableSelection = xlUnlockedCells
    End With
End Sub


使用 Tim Williams 的优化建议,您甚至可以跳过循环:
Option Explicit

Private Const PWD As String = "3827"
Private Const UNLOCK_COLS As String = "D1:K1,M1:P1,R1:S1,U1:V1"

Private Sub Worksheet_Activate()
    Dim dToday As Date, oRng As Range
    dToday = Date
    With ActiveSheet
        .Unprotect Password:=PWD
        .Cells.Locked = True
        ' Look for row with today's date and unlock the specific columns in the row
        Set oRng = .Columns("B").Find(What:=dToday)
        If Not oRng Is Nothing Then oRng.EntireRow.Range(UNLOCK_COLS).Locked = False
        .Protect Password:=PWD DrawingObjects:=False, Contents:=True, Scenarios:=True ' This allows Adding comments
        .EnableSelection = xlUnlockedCells
    End With
End Sub

【讨论】:

  • 非常感谢,我最近开始学习 VBA,你的代码好多了,完全解决了我的问题。非常感谢
  • 抱歉,有没有办法修改代码,以便允许使用工作表的人在需要时在解锁单元格上插入评论?
  • 我录制了宏来做你在保护期间需要做的事情,你只需要添加DrawingObjects:=False, Contents:=True, Scenarios:=True(见答案编辑)。但您可能还想在保护之前删除现有评论?
猜你喜欢
  • 1970-01-01
  • 2016-12-27
  • 2020-02-15
  • 1970-01-01
  • 1970-01-01
  • 2022-11-18
  • 1970-01-01
  • 2017-01-11
  • 1970-01-01
相关资源
最近更新 更多