仅复制单元格> 0

时间:2014-04-02 21:00:44

标签: excel vba copy

我是Macro新手 - 只是想出了如何添加开发人员标签,很抱歉,如果我的问题很愚蠢。我有列A中的项目列表和列B中的数量。我想将列A和B复制到列D和E,但仅当列B中的值> 0 - 我希望它们堆叠,数量= 0的空格没有空格。我在网上找到了一些代码:

Sub copyAboveZero()

    Dim sourceRng As Range
    Dim cell As Range
    Dim i As Long

    Set sourceRng = ActiveSheet.Range("B6:B24")
    i = 6

    For Each cell In sourceRng
        If cell.Value > 0 Then
            cell.Resize(1, 2).Copy Destination:=Range("D" & i)
            i = i + 1
        End If
    Next cell

End Sub

问题在于,在此示例中,数量位于第一个单元格中。这个是复制列B和C,我希望它复制A和B.我需要更改什么?此外,您只能粘贴特殊值吗?我不希望格式化它。

1 个答案:

答案 0 :(得分:0)

怎么样:

Sub KopyKat()
    Dim N As Long, i As Long
    Dim j As Long
    N = Cells(Rows.Count, "A").End(xlUp).Row
    j = 1
    For i = 1 To N
        If Cells(i, "B").Value > 0 Then
            Range(Cells(i, "A"), Cells(i, "B")).Copy Cells(j, "D")
            j = j + 1
        End If
    Next i
End Sub

修改#1

这可以解决您的意见:

Sub KopyKat()
    Dim N As Long, i As Long
    Dim J As Long
    N = Cells(Rows.Count, "A").End(xlUp).Row
    J = 6
    For i = 6 To N
        If Cells(i, "B").Value > 0 And Cells(i, "B") <> "" Then
            Range(Cells(i, "A"), Cells(i, "B")).Copy
            Cells(J, "D").PasteSpecial (xlValues)
            J = J + 1
        End If
    Next i
End Sub