Скопировать диапазон в последнюю строку и вставить в другой лист - PullRequest
0 голосов
/ 01 мая 2019

Мне нужен код 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 Ответ

2 голосов
/ 02 мая 2019

Я заметил, что у вас есть еще один вопрос, поэтому я тоже попробовал.

Я знаю, что этого не было в вашем исходном запросе, но я добавил раздел, который сначала очищает содержимое файла "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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...