gpt4 book ai didi

Excel 跟踪更改 VBA

转载 作者:行者123 更新时间:2023-12-04 21:07:34 24 4
gpt4 key购买 nike

我在用户表单和大量 VBA 方面有相当大的 excel。我在部分锁定一个工作表并同时允许 VBA 跟踪更改时遇到问题。
目前我使用下面的代码跟踪更改 - 此代码位于 Microsoft Excel 对象 >> 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”你所在的单元格或图表尝试更改是 protected ,因此是只读的。

最佳答案

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

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

关于Excel 跟踪更改 VBA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28196593/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com