【问题标题】:Excel Tracking Changes VBAExcel 跟踪更改 VBA
【发布时间】:2015-03-27 14:37:12
【问题描述】:

我在用户表单和大量 VBA 方面有相当大的 Excel。我在部分锁定一个工作表并同时允许 VBA 跟踪更改时遇到问题。 目前我使用下面的代码跟踪更改 - 此代码位于 Microsoft Excel Objects >> Sheet1 下:

Option Explicit
Public preValue As Variant
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
    Target.ClearComments
Target.AddComment.Text Text:="Previous Value was " & preValue & Chr(10) `& "Revised " & Format(Date, "mm-dd-yyyy") & Chr(10) & "By " & Environ`("UserName")
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target = "" Then
preValue = "a blank"
Else: preValue = Target.Value
End If
End Sub

另一段代码位于 Forms 文件夹中(我在其中创建了用户表单以获取用户的一些详细信息),如下所示:

Dim myPassword As String
myPassword = "123"
Set wsUK = Worksheets("Sheet1")
wsUK.Unprotect Password:=myPassword
' here there is a lot of code that throws data into Sheet1
wsUK.Protect Password:=myPassword

问题是用户表单完成后 Sheet1 受到部分保护,但我仍然允许用户更改 H 和 P 列中的数据。当我尝试这样做时,我得到运行时错误“1004”单元格或图表您尝试更改的内容受到保护,因此是只读的。

【问题讨论】:

    标签: excel vba tracking userform


    【解决方案1】:

    不要使用工作表保护方法,但仍会阻止用户更改您想要保护的单元格。

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Target.Count > 1 Then Exit Sub
    
    If Target.Column = 8 Or Target.Column = 16 Then
        Target.ClearComments
        Target.AddComment.Text Text:="Previous Value was " & preValue & Chr(10) & "Revised " & Format(Date, "mm-dd-yyyy") & Chr(10) & "By " & Environ("UserName")
    Else
        Application.EnableEvents = False
        Application.Undo
        Application.EnableEvents = True
    End If
    
    End Sub
    

    【讨论】:

    • 这不是答案。您的建议应包含在您的代码中以作为有效答案。基本上,您已经发表了评论,并且由于您还没有 50 个代表,因此您无法离开 cmets。我建议提供高质量的答案或提出高质量的问题,以获得 50 名代表离开 cmets。
    • 谢谢。如果您没有解决方案。试试我的解决方法。
    • 这是一个不错的选择 - 但是在一种情况下它会失败。我在第 1 到第 7 行有一个表格,我可以在表格正下方的行中输入文本 - 只有一个单元格保留文本 - 所有其他单元格都很好 - 我无法更改它们 - 任何想法为什么?感谢您的帮助!
    • 你能不能把它放在 Application.Undo 行之后: If Target.Row = 8 Then Target.ClearContents
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多