可变范围的粘贴范围返回语法错误

时间:2018-10-24 14:46:45

标签: excel vba excel-vba syntax copy-paste

我正在尝试遍历一组列,这些列的日期大小不一,并且有很多重复项。我想从TSX-DeltaFind复制列值,并将其粘贴到TSX-CleanDate中相应的“行情记录”下,并在完成后删除所有重复项。

A)为什么粘贴行上出现语法错误?
B)一旦粘贴这些副本,如何删除所有副本?

Sub CleanDate()
    Dim BottomRow As Long
    Dim BottomRow2 As Long
    Dim TopRow As Long
    Dim col As Integer
    Dim Ticker As String
    Dim RngY As Range
    Dim originalRng As Integer

    With ThisWorkbook.Worksheets("TSX-DeltaFind")
        col = 4
        TopRow = 6

        For col = 4 To 3 + (2 * 26) Step 2
            Ticker = .Cells(TopRow - 1, col - 2).Value
            BottomRow = .Cells(.Rows.Count, col).End(xlUp).row
            originalRng = BottomRow - TopRow

            Worksheets("TSX-DeltaFind").Range(.Cells(TopRow, col), .Cells(BottomRow, col)).Copy
            Worksheets("TSX-CleanDate").Activate

            With ThisWorkbook.Worksheets("TSX-CleanDate")
                Set RngY = Worksheets("TSX-CleanDate").Range("A3:XDF3").Find(Ticker, lookat:=xlPart)
                BottomRow2 = .Cells(.Rows.Count, RngY.Column).End(xlUp).row

                Worksheets("TSX-CleanDate").Range(BottomRow2 + 1, RngY.Column + 2:originalRng + 4,RngY.Column + 2 ).PasteValues
            End With

            Worksheets("TSX-DeltaFind").Activate
        Next
    End With
End Sub

1 个答案:

答案 0 :(得分:1)

对于A),范围参数应类似于range(“ a2:b2”)而不是range(1,2:2,2)

您可以尝试使用Cell来引用它们。例如

Worksheets("TSX-CleanDate").Range(BottomRow2 + 1, RngY.Column + 2:originalRng + 4,RngY.Column + 2 ).PasteValues

可能是

Worksheets("TSX-CleanDate").Range(Cells(BottomRow2 + 1, RngY.Column + 2),Cells(originalRng + 4,RngY.Column + 2)).PasteSpecial (xlPasteValues)

B)对于重复项,请使用range.removeduplicates