最后一行粘贴到不同的工作表VBA [英] Last Row Paste to different Worksheet VBA

查看:79
本文介绍了最后一行粘贴到不同的工作表VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将信息粘贴到其他工作表的最后一行.我已经复制了信息,此宏只是将其粘贴.使用我现在拥有的代码,我收到一个错误,提示工作表类的粘贴方法失败",我该如何解决呢?

I am trying to paste info on the last row of a different worksheet. I already have the info copied and this macro is just to paste it. With the code that I have now, I am getting an error that says "Paste Method of Worksheet Class Failed" how can I fix this?

下面是代码:

Windows("m.xlsx").Activate
Cells(Application.Rows.Count, 1).End(xlUp).Offset(1, 0).Select
ActiveSheet.Paste
Range("D45").Select
Windows("d.xlsx").Activate

推荐答案

众所周知,Excel可以清除剪贴板,因此在粘贴之前,应将复制命令放在一行.

Excel is known to clear the clipboard and hence you should put the copy command one line before you paste.

因此,在您的情况下,复制命令将排在前面

So in your case the copy command will go before

ActiveSheet.Paste

也请避免使用.选择/激活.请阅读如何避免在Excel VBA宏中使用选择"

Also please avoid the use of .Select/Activate. Please read How to avoid using Select in Excel VBA macros

这是您要尝试的(未经测试)吗?

Is this what you are trying (Untested)?

Sub Sample()
    Dim thisWb As Workbook, thatWb As Workbook
    Dim lRow As Long

    Set thisWb = Workbooks("d.xlsx")
    Set thatWb = Workbooks("m.xlsx")

    '~~> Change Sheet1 to the relevant sheet
    With thatWb.Sheets("Sheet1")
        '~~> Find last Row to paste
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row + 1

        '~~> Change Sheet1 to the relevant sheet
        '~~> Replace A1:A10 with the relevant range that you are trying to copy
        thisWb.Sheets("Sheet1").Range("A1:A10").Copy .Range("A" & lRow)
    End With
End Sub

这篇关于最后一行粘贴到不同的工作表VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆