运行时错误1004复制期间应用程序定义或对象定义的错误

时间:2018-04-09 12:42:43

标签: excel-vba vba excel

我在使用范围复制第24个(总是24!)记录(正确复制范围中的前24个记录)后,得到运行时错误1004应用程序定义或对象定义错误。当我使用EntireRow进行复制时,宏工作正常并正确完成。

调试显示cell.Offset(, -2).Resize(, 15).Copy发生错误,而cell.EntireRow.Copy工作正常。

第24,25,26等行之间没有什么不同,我删除了行以确保没有损坏 - 没有变化: - (

不幸的是,我不想复制整行,因为这会阻止我在粘贴数据时偏移到不同的列,我需要为要添加的操作执行此操作。所以我需要使用范围。

对于解决方法有什么建议吗?我重新启动并复制到一个干净的电子表格中 - 没有变化!

Sub Create_Event_ID_list()
'Looks up last name in column A in Sheet1
'Finds matching last name in column C in Sheet2
'Copies cells in cols A to O in row with matching name
'Pastes copied cells into new row in Sheet3

Dim cell        As Range
Dim Last_name   As Variant
Dim LastRow     As Long
Dim NextRow     As Long

    Application.ScreenUpdating = False
    Application.EnableEvents = False

    With Worksheets("Sheet1")

        LastRow = .Cells(Rows.Count, "A").End(xlUp).Row

        For Each Last_name In Sheets("Sheet1").Range("A2:A" & LastRow).Cells

            Set cell = Sheets("Sheet2").Cells.Find(Last_name, , xlValues, xlWhole, xlByRows, False, False, False)

            If Not cell Is Nothing Then

                cell.Offset(, -2).Resize(, 15).Copy Destination:=Worksheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Offset(1)
                'Gives Run-time error 1004 Application-defined or object-defined error in 24th record

                'cell.EntireRow.Copy Destination:=Worksheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Offset(1) 
                'Works fine

            End If

        Next Last_name
    End With
    Application.ScreenUpdating = True
    Application.EnableEvents = True

End Sub

0 个答案:

没有答案