如果单元格有值复制到下一列同一行vba代码

时间:2015-02-11 16:25:21

标签: excel-vba vba excel

如果A = {{1} {{1}将B复制到A14

我有这段代码,但大多数情况下,"zois"中的空白单元格会在"zois"中删除未复制的值。

B14

1 个答案:

答案 0 :(得分:0)

您在 A 列中输入的内容将被复制到 B 列(甚至空白!!)

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim A As Range, intr As Range, r As Range
    Set A = Range("A:A")
    Set intr = Intersect(A, Target)
    If intr Is Nothing Then Exit Sub

    Application.EnableEvents = False
        For Each r In intr
            r.Copy r.Offset(0, 1)
        Next r
    Application.EnableEvents = True
End Sub

修改#1:

如果 A 中的值被清除,此版本将清除 B 列中的值:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim A As Range, intr As Range, r As Range
    Set A = Range("A:A")
    Set intr = Intersect(A, Target)
    If intr Is Nothing Then Exit Sub

    Application.EnableEvents = False
        For Each r In intr
            If r.Value <> "" Then
                r.Copy r.Offset(0, 1)
            End If
        Next r
    Application.EnableEvents = True
End Sub

在安装新版本之前,请务必删除旧版本。