我正在尝试为版本跟踪创建电子表格,但是有一个单独的表格捕获第一张表格的更改,需要包含一些其他信息。这是当前正在运行的脚本。
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Sh.Name = "Log" Then Exit Sub
Application.EnableEvents = False
On Error Resume Next
With Sheets("Log").Cells(Rows.Count, 1).End(xlUp)
.Offset(1, 0).Value = Environ("UserName")
.Offset(1, 1) = Sh.Name
.Offset(1, 3) = Target.Address
.Offset(1, 4) = "'" & Target.Formula
.Offset(1, 5) = Previous
Previous = ""
.Offset(1, 6) = Now
End With
Application.EnableEvents = True
End Sub
修改的单元格在第3列中作为Target.Address捕获。我希望能够引用Target.Address的Row并在第一个工作表的同一行显示A列的值。 。
例如:在工作表1上修改了单元格D7。日志表显示$ D $ 7作为目标地址的值。在另一个单元格中,我想知道A7的值是什么感觉target.address的行是7.它将位于.Offset(1,2)=示例中缺少的点,因为我正在尝试不同的公式而不是使用vba。
我很害怕,我很难找到并且很难找到或措辞问题。
任何建议都将不胜感激! 提前谢谢你,
答案 0 :(得分:1)
处理多个已更改的单元格:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Const LOG_SHEET As String = "Log"
Dim c As Range
If Sh.Name = LOG_SHEET Then Exit Sub
For Each c In Target.Cells
With Sheets(LOG_SHEET).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).EntireRow
.Cells(1).Value = Environ("UserName")
.Cells(2).Value = c.EntireRow.Cells(1).Value 'server name
.Cells(3).Value = Sh.Name
.Cells(4) = c.Address(False, False)
.Cells(5) = "'" & c.Value
.Cells(6) = Now
End With
Next c
End Sub
你不需要在这里禁用事件,因为"退出if log sheet"专线负责处理。
答案 1 :(得分:0)
您只需使用Target.Value2
属性即可解决更改后的值。你的脚本看起来像这样:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Sh.Name = "Log" Then Exit Sub
Application.EnableEvents = False
On Error Resume Next
With Sheets("Log").Cells(Rows.Count, 1).End(xlUp)
.Offset(1, 0).Value = Environ("UserName")
.Offset(1, 1) = Sh.Name
.Offset(1, 2) = Target.Value2
.Offset(1, 3) = Target.Address
.Offset(1, 4) = "'" & Target.Formula
.Offset(1, 5) = Previous
Previous = ""
.Offset(1, 6) = Now
End With
Application.EnableEvents = True
End Sub