简体   繁体   中英

copy/ paste data to/after the last row/column of data. from old workbook to new workbook

I am updating a weekly Master file. As a result, I am trying to copy and paste the data from the source file to the Master sheet. Now, The data needs to be pasted to the columns and rows after the data of the previous week. So for example if last weeks data ends ("H70:M70"), I need the new pasted data to go to ("H71:M71"). Also, the new pasted data has many rows, so nexts week data has to be pasted after the last row/column of this weeks data ie ("H230:M230"). Please Help.

Option Explicit
Sub Copydata()
Dim myWB As Workbook
Dim thisWB As Workbook
Dim thisWS As Worksheet

Application.ScreenUpdating = False    

Set myWB = Workbooks.Open("C:\Users\jjordan\Desktop\test.xlsx")

myWB.Sheets("Sheet1").Range("A8:F17").Copy

Set thisWB = ThisWorkbook
Set thisWS = thisWB.Sheets("Test data")

thisWB.Activate
thisWS.Range("H71:M71").PasteSpecial _
    Paste:=xlPasteValues, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False

With Application
    .CutCopyMode = False
    .ScreenUpdating = True
End With

myWB.Close

End Sub

James, are you familiar with End() in VBA?

lastRow = ActiveSheet.Cells(Rows.Count,1).End(xlUp).Row

This will go to the last possible Cell in column 1 of your worksheet, then go up until it finds a Cell containing data and return the corresponding row.

The other possible arguments for End() are xlDown, xlToLeft, and xlToRight. They can be used to find last/first columns and rows.

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