[英]I keep getting a 1004 Error Application-Defined or Object-Defined Error when trying to copy and paste
I'm trying to copy Range A:3 to A:x (where x is the number of rows in the table "AgentName") from sheet "AgentName" However, whenever I try to copy the range with a variable in it, it gives me the Error 1004. I've read through everything I could find. 我正在尝试从工作表“ AgentName”中将范围A:3复制到A:x(其中x是表“ AgentName”中的行数),但是,每当我尝试复制包含变量的范围时,它给我错误1004。我已经阅读了所有可以找到的内容。 Here's two common ones I've tried. 这是我尝试过的两个常见问题。
Worksheets("AgentName").Range("A3:A"&x).copy
Worksheets("AgentName").Range(Sheets("AgentName").Cells(3, 1), Sheets("AgentName").Cells(x, 1)).Copy
^I've tried it without the Sheets, with just cells, with cells and range together. ^我尝试了没有表格,仅包含单元格,包含单元格和范围的表格。
Here's a shortened version of the code: 这是代码的简化版:
Private Sub CommandButton1_Click()
'CountRows in AgentName Table
Dim myWorkSheet As Worksheet, myTable As ListObject, x As Long
Set myWorkSheet = ActiveWorkbook.Worksheets("AgentName")
Set myTable = myWorkSheet.ListObjects("AgentName")
x = myTable.DataBodyRange.Rows.Count
'Specific Forms for All Agents (Motivated)
If Range("B3").Value = "All Agents" And Range("C3").Value = "Motivated" Then
Worksheets("Other Forms").Range("A1:H2").Copy
Worksheets("Master").Range("B6:I7").PasteSpecial
Worksheets("AgentName").Range(Sheets("AgentName").Cells(3, 1), Sheets("AgentName").Cells(x, 1)).Copy
Worksheets("Master").Range("B8:B13").PasteSpecial
Range("B6:I13").Borders.LineStyle = xlContinuous
End If
End Sub
I see two potential sources of error: first of all you copy and paste a range with non constant size into a range with constant size namely here: 我看到两个潜在的错误源:首先,将非恒定大小的范围复制并粘贴到具有恒定大小的范围内,即:
Worksheets("AgentName").Range(Sheets("AgentName").Cells(3, 1), Sheets("AgentName").Cells(x, 1)).Copy
Worksheets("Master").Range("B8:B13").PasteSpecial
You paste A3:Ax into B8:B13 if x is not equal to 8 this will give you an error so just paste it into the first cell and excel will assume that you will want to paste the rest just below. 如果x不等于8,则将A3:Ax粘贴到B8:B13中,这将给您带来错误,因此只需将其粘贴到第一个单元格中,excel就会假定您将要粘贴其余的内容。 So : 因此:
Worksheets("Master").Range("B8").PasteSpecial
instead. 代替。
The second point is your construction of copying: 第二点是复制的结构:
Worksheets("AgentName").Range(Sheets("AgentName").Cells(3, 1), Sheets("AgentName").Cells(x, 1)).Copy
I would rather work with the listobject if you have one already defined. 如果您已经定义了listobject,我宁愿使用listobject。
mytable.databodyrange(1,mytable.listrows.count).copy
This or something very similar should work. 这或非常相似的东西应该起作用。 Be aware that .row gives you the absolute row and listrow will give you the row relative to the table. 请注意,.row为您提供了绝对行,listrow为您提供了相对于表的行。 So mytable.databodyrange(1,3)
will be the entry in the first row and the third column of the table no matter were the table is. 因此,无论表是什么, mytable.databodyrange(1,3)
都将是表的第一行和第三列中的条目。 This may be absolute row 9053 and absolute column 456. It is usually better to work with the relative coordinates like this you will be able to use code again for other purposes and you just need to adjust the table. 这可以是绝对行9053和绝对列456。通常最好使用这样的相对坐标,您将能够再次将代码用于其他目的,并且只需要调整表格即可。
Good Luck I hope this was of help! 祝您好运,希望对您有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.