简体   繁体   English

将行从一张纸复制到另一张纸

[英]Copying rows from one sheet to another

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? 有什么建议么?

Resize is not like OFFSET. 调整大小不像偏移。 It will set the size of the range to the size dictated. 它将范围的大小设置为指定的大小。 So you are setting the range size to 0 rows. 因此,您将范围大小设置为0行。 It should be 1: 应该是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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