简体   繁体   English

VBA Excel宏.copy目标错误1004

[英]VBA Excel Macro .copy destination Error 1004

i hope someone can help me. 我希望有一个人可以帮助我。

I tried everything, but i cant fix the following code: 我尝试了所有操作,但无法修复以下代码:

Sheets(Sheets.Count).Range("A2:J2" & Range("A2:J2").End(xlDown).Row).Copy _
    Destination:=Sheets(Sheets.Count - 3).Range("A2")

If i leave the destination line and use the following code instead, it works. 如果我离开目标行并改为使用以下代码,则它可以工作。

Sheets(Sheets.Count).Range("A2:J2" & Range("A2:J2").End(xlDown).Row).Copy '_
    'Destination:=Sheets(Sheets.Count - 3).Range("A2")

Sheets("All Docs " & Date).Select '(All Docs " & Date) = (Sheets.count-3)
Range("A2").Select
ActiveSheet.Paste

Any suggestions? 有什么建议么? Thx a lot. 多谢。

I think I see what's happening. 我想我知道发生了什么事。 You're trying to get a "block" of data, from A2:J2 across, and then to some row going down, say A10:J10 - to end up with a range A2:J10 that you want to copy. 您正在尝试获取一个数据块,从A2:J2开始,然后向下延伸到某行,例如A10:J10-最终得到要复制的范围A2:J10

What I think might be happening is there is no data (or perhaps not enough) under your Row 2, so Excel's getting your .End(xlDown.Row as 1048576 . 我认为可能正在发生的是,第2行下没有数据(或可能不够),因此Excel .End(xlDown.Row您的.End(xlDown.Row1048576

Try using this: 尝试使用此:

Sub test()
Dim lastRow As Long
With Sheets(Sheets.Count)
    lastRow = .Range(.Cells(2, 1), .Cells(2, 10)).End(xlDown).Row

    If lastRow >= 1048576 Then
        lastRow = 2
    Else
        lastRow = .Range(.Cells(2, 1), .Cells(2, 10)).End(xlDown).Row
    End If

    .Range(.Cells(2, 1), .Cells(lastRow, 10)).Copy Destination:=Sheets(Sheets.Count - 3).Range("A2")

End With

End Sub

Note about using Sheets(Sheets.Count) . 关于使用Sheets(Sheets.Count)注意事项。 This is not the same as using say, Sheets("LastSheet") . 这是一样的使用比方说, Sheets("LastSheet") If you change the order of your worksheets around, this will affect where your data is pasted into. 如果更改工作表的顺序,这将影响数据粘贴到的位置。 IE if your right most sheet has your data (which I think it does, since you're using it to get the info to paste), and you move it anywhere else but the end of the file, then your data won't properly be found. IE,如果最右边的工作表包含您的数据(我想是的,因为您正在使用它来粘贴信息),并且将其移动到文件末尾以外的其他位置,则您的数据将无法正常工作被发现。 Sheets.Count refers to the very last (right most) sheet. Sheets.Count是指最后(最右边)的工作表。 And Sheets.Count - 3 refers to the sheet three over from the end (again, moving that sheet will also result in not pasting where you want). Sheets.Count - 3指的是末尾的三张纸(同样,移动纸也将导致无法粘贴到您想要的位置)。

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

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