Copying rows from one sheet to another

时间:2016-06-10 16:11:27

标签: excel vba

The following script seems like it should work, but I'm getting an "Object defined" error on the lines marked below. I can't find what's causing this at all...

Sub MailMerge()
Sheets.Add.Name = "MailMerge"
Dim MailMerge As Worksheet
Set MailMerge = Sheets("MailMerge")
Dim Rng As Range
Dim i, index, lastrow As Long
Dim Abstracts As Worksheet
Set Abstracts = Sheets("Abstracts")

lastrow = Abstracts.Cells(Rows.Count, 1).End(xlUp).row


For i = 1 To lastrow
    Set Rng = Abstracts.Range("O" & i)
        If WorksheetFunction.CountA(Rng) >= 1 Then
            Abstracts.Range("A" & i).Resize(0, 14).Copy _
            Destination:=MailMerge.Range("A" & i).Resize(0, 14)
            'this is where the error is occuring
         End If
Next

End Sub

Any suggestions?

1 个答案:

答案 0 :(得分:1)

调整大小不像OFFSET。它会将范围的大小设置为指定的大小。因此,您将范围大小设置为0行。它应该是1:

Sub MailMerge()
Sheets.Add.Name = "MailMerge"
Dim MailMerge As Worksheet
Set MailMerge = Sheets("MailMerge")
Dim Rng As Range
Dim i, index, lastrow As Long
Dim Abstracts As Worksheet
Set Abstracts = Sheets("Abstracts")

lastrow = Abstracts.Cells(Rows.Count, 1).End(xlUp).Row


For i = 1 To lastrow
    Set Rng = Abstracts.Range("O" & i)
        If WorksheetFunction.CountA(Rng) >= 1 Then
            Abstracts.Range("A" & i).Resize(1, 14).Copy _
            Destination:=MailMerge.Range("A" & i).Resize(1, 14)
            'this is where the error is occuring
         End If
Next

End Sub