将范围复制到最后一行并粘贴到另一个工作表中

时间:2019-05-01 20:45:07

标签: excel vba

我需要vba代码来复制从A5开始的所有内容,将其右边的所有内容复制到一个工作簿中的最后一行,并将其粘贴到从A5开始的另一工作簿中。

这是我到目前为止的代码,但是没有用。

Windows("Month By Month Income Statment 10.xlsx").Activate
Sheets("Month By Month Income Statmen-A").Select
Range("A5").Select

Dim EC As Long
Dim X As Long
Dim Y As Long
X = Range("A5").End(xlUp).Offset(1, 0).Row
EC = Range("A5").End(xlToLeft).Offset(0, X).Column

Range("A5" & EC).Select
Selection.Copy

Windows("RPG - Apr Mnth acs by co.xlsm").Activate
Sheets("010 - RPL").Select
Range("A5").Select
ActiveSheet.Paste

End Sub

1 个答案:

答案 0 :(得分:2)

我注意到您还有另一个问题要回答,所以我也很想解决这个问题。

我知道它不在您的原始请求中,但是我添加了一个部分来首先清除“ RPG”文件的内容(仅从第5行向下),这样您就不会遇到任何问题问题。这样,您将始终有一个空白页可将新数据粘贴到其中,并且如果新数据小于旧数据,则永远不会保留上次的数据。如果您不需要,请随时将其忽略。

Sub Get_New_Data_From_Other_Workbook()
'
' This macro will copy data from a .xlsx file and paste it back into the .xlsm file
' Any contents in the .xlsm file will first be deleted

    ' Clear the existing contents of the destination sheet
    Windows("RPG - Apr Mnth acs by co.xlsm").Activate                   ' Make sure the RPG file is selected
    Sheets("010 - RPL").Select                                          ' Select the required sheet
    Range("A5").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select        ' From A5, select every cell until the end of the page (up to where the data stops)
    Application.CutCopyMode = False
    Selection.ClearContents                                             ' Delete contents
    Range("A1").Select                                                  ' Select A1 for presentation purposes

    ' Go to the correct sheet of the other workbook and copy the data
    Windows("Month By Month Income Statement 10.xlsx").Activate         ' Select the other workbook
    Sheets("Month By Month Income Statmen-A").Select                    ' Select the sheet with the data on
    Range("A5").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select        ' From A5, select every cell until the end of the page (up to where the data stops)
    Selection.Copy                                                      ' Copy the data

    ' Come back to the macro workbook and paste the data in A5 of the required sheet
    Windows("RPG - Apr Mnth acs by co.xlsm").Activate                   ' Select the RPG file
    Sheets("010 - RPL").Select                                          ' Select the required sheet
    Range("A5").Select                                                  ' Select cell A5
    ActiveSheet.Paste                                                   ' Paste the data
    Range("A1").Select                                                  ' Select A1 for presentation purposes

End Sub