粘贴位置范围的问题

时间:2016-05-19 08:28:12

标签: excel vba excel-vba

您好我有一个代码,它根据给定的条件复制列,并粘贴到另一个工作表。我想更改粘贴位置,因此应该从"C5" WF-4 CC with Values表开始粘贴,但我无法这样做。它给出了选择方法失败错误。如何更改粘贴范围位置需要一些帮助。谢谢:)这是代码:

Sub ExtractCCData4()

    Dim lastcol As Long
    Dim j As Long

    With Worksheets("WF - L4")
        lastcol = .Cells(5, Columns.Count).End(xlToLeft).Column

        For j = 3 To lastcol
            If Application.WorksheetFunction.Sum(.Columns(j)) > 0 Then
                .Columns(j).Copy Destination:=Worksheets("WF-4 CC with  Values").Columns(j)
            Else

            End If
        Next
    End With

End Sub

1 个答案:

答案 0 :(得分:1)

在OP的澄清之后

编辑

我假设

  • “它应该从”C5“开始粘贴意味着它从”C5“向右无缝地粘贴,而不考虑”WF - L4“列跳过

  • 您只需要粘贴值

然后尝试这个

Option Explicit

Sub ExtractCCData4()

    Dim lastcol As Long
    Dim j As Long, jPaste As Long
    Dim rngToCopy As Range

    With Worksheets("WF - L4")
        lastcol = .Cells(5, .Columns.Count).End(xlToLeft).Column

        For j = 3 To lastcol
            If Application.WorksheetFunction.Sum(.Columns(j)) > 0 Then
                Set rngToCopy = .Range(.Cells(1, j), .Cells(.Rows.Count, j).End(xlUp))

                Worksheets("WF-4 CC with  Values").Range("C5").Offset(, jPaste).Resize(rngToCopy.Rows.Count).Value = rngToCopy.Value
                jPaste = jPaste + 1 '<~~ updating column offsetting from "WF - L4" sheet column "C"
            Else

            End If
        Next
    End With

End Sub