跟踪工作表上的更改,复制活动单元格行中非活动单元格的单元格并记录值

问题描述

我能够根据我的需要调整以下公式并且运行良好。我确实看到我需要添加一行来帮助锁定稍后发生的计算。如何编写复制单元格更改活动行的列“R”中的值并继续将逻辑放置在 LogDetails 工作表上的列 18 中的值?

代码位于工作表中:


Option Explicit
Public OldValue As String
         
Private Sub Worksheet_Change(ByVal Target As Range)
Dim sSheetName As String
    
    sSheetName = "Capital"
    
    Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(1,0).value = ActiveSheet.Name
    Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0,1).value = Environ("username")
    Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0,2).value = Target.Address(0,0)
    Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0,10).value = Now
    Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0,14).value = OldValue
    Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0,15).value = Target.value
                 
    Application.EnableEvents = False
    Application.EnableEvents = True
    
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Selection.Cells.Count > 1 Then
        MsgBox "Multiple cell selections are not allowed on this sheet",vbCritical
        ActiveCell.Select
   Exit Sub
End If
   
    On Error Resume Next
    OldValue = Target.value
 
End Sub

欣赏和见解……!

解决方法

下面的代码包含了我通过您问题下的评论向您提出的建议。

我还稍微简化了代码...

Option Explicit
Public OldValue,OldColumnRValue

     
Private Sub Worksheet_Change(ByVal Target As Range)        
    Application.EnableEvents = False        
        With Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp)
            .Offset(1) = ActiveSheet.Name
            .Offset(,1) = Environ("username")
            .Offset(,2) = Target.Address(0,0)
            .Offset(,10) = Now
            .Offset(,14) = OldValue
            .Offset(,15) = Target
            .Offset(,18) = OldColumnRValue
        End With                     
    Application.EnableEvents = True        
End Sub


Private Sub Worksheet_SelectionChange(ByVal Target As Range)    
    If Selection.Cells.Count = 1 Then
        OldValue = Target
        OldColumnRValue = Range("A1")(Target.Row,18)
        Exit Sub
    End If    
    MsgBox "Multiple cell selections are not allowed on this sheet",vbCritical
    ActiveCell.Select    
End Sub
,

首先让我们不要重复自己。使用 With 关键字处理对象。然后我们将在我们的选择更改中添加一个动态偏移量,以记录所选行的 R 列中的值。我们将在您的 LogDetail 中使用它来将值放在第 18 列中。

Option Explicit
Public oldValue As Variant
Public currentRvalue As Variant
         
Private Sub Worksheet_Change(ByVal Target As Range)
Dim sSheetName As String
    
    sSheetName = "Capital"
    
    With Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp)
        .Offset(1,0).Value = ActiveSheet.Name
        .Offset(0,1).Value = Environ("username")
        .Offset(0,2).Value = Target.Address(0,0)
        .Offset(0,10).Value = Now
        .Offset(0,14).Value = oldValue
        .Offset(0,15).Value = Target.Value
        .Offset(0,18).Value = currentRvalue
    End With
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Selection.Cells.Count > 1 Then
            MsgBox "Multiple cell selections are not allowed on this sheet",vbCritical
            ActiveCell.Select
       Exit Sub
    End If
   
    oldValue = Target.Value
    currentRvalue = Target.Offset(0,18 - Target.Column).Value
End Sub