简体   繁体   中英

Error 1004: Excel VBA

The error occurs at ActiveSheet.Cells(q,1).Offset(2,1).PasteSpecial.xlPasteValues

Was wondering why I am faced with a runtime error 1004. It mentions that the information cannot be pasted as copy area and paste area are not the same size and shape. Is there a work around?

                     For i = 1 To testrows
                            For j = 1 To testcols

                                If (range1.Cells(i, j).Value <> range2.Cells(i, j).Value) Then
                                     'Conclude that range dimension is not the same
                                     bMatches = False
                                     i = testrows
                                     j = testcols
                                     'Exit loops
                                End If
                            Next
                        Next
                    End If

                    'If ranges of two comparison sheets are the same
                    If bMatches Then
                        rowmatched = True
                        k = referencesheetcols
                    End If

                    'Sheets(outputsheetname).Cells(1, 1).Value = rowmatched
                    'Set place to paste data
                    If (Not (rowmatched) And k = referencesheetcols) Then
                        range2.Copy
                        Sheets(referencesheetname).Cells(p, 1).Offset(2, 0).Select
                        ActiveSheet.Paste
                        p = p + 1
                        Sheets("Datasheet").Activate
                        'ActiveSheet.Cells(q, 1).Offset(2, 1).Select
                        ActiveSheet.Cells(q, 1).Offset(2, 1).PasteSpecial xlPasteValues
                        'ActiveSheet.PasteSpecial xlPasteValues
                        q = q + 1
                    End If
                    Next
            End If
        Next

    End Sub`enter code here`
'.....
If (Not (rowmatched) And k = referencesheetcols) Then

    range2.Copy Sheets(referencesheetname).Cells(p, 1).Offset(2, 0)
    p = p + 1

    Sheets("Datasheet").Cells(q, 1).Offset(2, 1).Resize( _
        range2.Rows.Count, range2.Columns.Count).Value = range2.Value
    q = q + 1

End If
'.....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM