简体   繁体   中英

Copy Data from Active cells to Another Workbook

I'm a total beginner here. I saw this example online to copy data from a fixed range of a workbook to another workbook.

Could someone please guide me on how I can copy from A2 cell onward to the last active cell with data?这是我需要复制的数据示例 .

In this given example, I wanted to copy those cells highlighted in blue to another workbook. The data varies every time, hence a fixed range doesn't work for me. I try using ".UsedRange" but it copies the footer as well, which is something I don't need.

Sub Copy_Method()
  Workbooks("New-Data.xlsx").Worksheets("Export").Range("A2:D9").Copy _
    Workbooks("Reports.xlsm").Worksheets("Data").Range("A2")

 End Sub

Ref: https://www.excelcampus.com/vba/copy-paste-another-workbook/

as per the posted screenshot:

  • the "footer" appears to be placed in column A
  • data fill all columns from A to E

hence you can use column B to get the last used cell row from:

Sub Copy_Method()
   With Workbooks("New-Data.xlsx").Worksheets("Export")
       .Range("A2:E" & .Cells(.Rows.Count, 2).End(xlUp).Row).Copy _
       Workbooks("Reports.xlsm").Worksheets("Data").Range("A2")
   End With
 End Sub

Given your particular set up, you can use the CurrentRegion property.

Sub Copy_Method()

Dim r As Range

Set r = Workbooks("New-Data.xlsx").Worksheets("Export").Range("A1").CurrentRegion
Set r = r.Offset(1).Resize(r.Rows.Count - 1) 'exclude the header
r.Copy Workbooks("Reports.xlsm").Worksheets("Data").Range("A2")

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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