繁体   English   中英

更改时将单元格复制到另一列

[英]Copy Cell to another column on change

我需要在更改时将特定列中单元格的内容复制到另一个相应的列,以便移动旧值。 只想为特定列工作。

Private sub Worksheet_Change(ByVal Target As Range)

if Target.Range("L:L") then
'set I cell value = to original L cell value
ActiveCell.Offset(0,-3).Value = ActiveCell.Value
End If

End Sub

这段代码应该做你想做的事。 请注意解释我对该程序的操作施加的一些限制的评论。 遵循的规则是不要给它比它完成你想要它做的工作所需的更多的权力。

Private Sub Worksheet_Change(ByVal Target As Range)
    ' 027
    Dim Rng As Range

    ' don't react if the changed cell is in row 1 or
    '   if it is more than 1 row below the end of column L
    Set Rng = Range(Cells(2, "L"), Cells(Rows.Count, "L").End(xlUp).Offset(1))

    If Not Application.Intersect(Target, Rng) Is Nothing Then
        With Target
            ' skip if more than 1 cell was changed
            '   meaning, exclude paste actions
            If .Cells.CountLarge = 1 Then
                .Offset(0, -3).Value = .Value
            End If
        End With
    End If
End Sub

这将保存第I列中的先前值:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim v As Variant
    If Target.Count > 1 Then Exit Sub
    If Intersect(Range("L:L"), Target) Is Nothing Then Exit Sub
    With Application
        v = Target.Value
        .EnableEvents = False
        .Undo
        Target.Offset(0, -3).Value = Target.Value
        Target.Value = v
        .EnableEvents = True
    End With
End Sub

编辑#1:

要在不触发事件的情况下更新L ,请使用以下内容:

Sub ScriptThatUpdatesColumn_L()
    Application.EnableEvents = False
        Range("L5").Value = "just me"
    Application.EnableEvents = True
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM