Excel VBA将单元格复制到更改时的另一列中

问题描述 投票:0回答:1

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

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
excel vba excel-vba excel-2016
1个回答
0
投票

此代码应执行您想要的操作。请注意注释,这些注释解释了我对该程序的操作施加的一些限制。遵循的规则是,不要赋予它超出执行所需功能的能力。

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
© www.soinside.com 2019 - 2024. All rights reserved.