Excel单元格仅通过VBA脚本进行修改

时间:2017-09-28 05:26:51

标签: vba excel-vba select-case excel

我正在制作一个vba脚本。每次双击相关单元格时,它都会应用时间戳。双击后,单元格被锁定。

当再次双击该单元格时,会出现一个弹出框,要求输入密码,这很好。

问题:但是我希望细胞不受保护直到再次双击。

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

With Target
    If .Column = 4 Then
        Select Case .Row
            Case 20, 24, 25, 27, 28, 30 - 35, 37, 38, 40, 42 - 44, 54 - 56, 58, 59, 61 - 65
               ActiveSheet.Protect Password:="Test", userinterfaceonly:=True
                .Value2 = "Prepared By" & "  " & Environ("Username")
                .Value2 = .Value2 & "  " & Format(Now, "yyyy-MM-dd hh:mm:ss")
                .Locked = True
                ActiveSheet.Unprotect
        End Select
    End If
End With

按照以下答案编辑,但是用户可以双击仍然更改的保护单元输入密码。

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    Application.EnableEvents = False

    With Target
        If .Column = 4 Then
            Select Case .Row
                Case 20, 24, 25, 27, 28, 30, 31, 32, 33, 34, 35, 37, 38, 40, 42, 43, 44, 54, 55, 56, 58, 59, 61, 62, 63, 64, 65
                    If InStr(1, Target.Value2, "@@") = 0 Then
                        Target.Value2 = Target.Value2 & "@@"
                    Else
                        ActiveSheet.Protect Password:="Test", userinterfaceonly:=True
                        .Value2 = "Prepared By" & "  " & Environ("Username")
                        .Value2 = .Value2 & "  " & Format(Now, "yyyy-MM-dd hh:mm:ss")
                        .Locked = True
                        ActiveSheet.Unprotect
                    End If
            End Select
        End If
    End With

0 个答案:

没有答案