首页 > 解决方案 > 更改时将单元格复制到另一列

问题描述

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

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

标签: excelvba

解决方案


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

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

推荐阅读