Excel VBA:范围类的复制方法失败

时间:2014-10-08 14:12:04

标签: excel vba excel-vba

这是代码:

Dim rngReason As Range, rngDest As Variant

Set rngDest = wksRemoved.Range("A1").CurrentRegion
Set rngDest = rngDest.Cells(rngDest.Rows.Count + 1, 1)

Set rngReason = Range("A1").CurrentRegion
Set rngReason = rngReason.Columns(rngReason.Columns.Count).offset(0, 1)
Set rngReason = Intersect(rngReason, rngDupeRows)

rngReason.Cells.Value2 = "Duplicate " & strDedupeField

''Fails here with the error message:
''  Copy method of range class failed
rngDupeRows.Copy Destination:=rngDest 

rngDupeRows是一个范围。错误发生时rngDupeRowsrngDest地址的值为:

rngDest.Address      "$A$2"       Variant/String
rngDupeRows.Address  "$3:$98301"  String

代码的目的是从一个工作簿复制行并将其粘贴到另一个工作簿中。我在不同的范围内多次运行它并且运行正常。

任何想法可能是错误的原因是什么?

1 个答案:

答案 0 :(得分:0)

可能需要很长时间,但如果必须这样做,那么您可以尝试添加一个循环来为范围内的每个单元格指定值,而不是使用“复制”

        Dim rngReason As Range, rngDest As Variant
        Set rngDest = wksRemoved.Range("A1").CurrentRegion
        Set rngDest = rngDest.Cells(rngDest.Rows.Count + 1, 1)

        Set rngReason = Range("A1").CurrentRegion
        Set rngReason = rngReason.Columns(rngReason.Columns.Count).offset(0, 1)
        Set rngReason = Intersect(rngReason, rngDupeRows)

        rngReason.Cells.Value2 = "Duplicate " & strDedupeField

       'rngDupeRows.Copy Destination:=rngDest
        ' Try messing around with something like this code.
        Dim rngTmp As Range
        For Each rngTmp In rngDest
            rngTmp.value = rngDupeRows.Value
        Next
        '***