[英]Copy data from one sheet to the last row of another sheet
我正在嘗試將數據從一張紙復制到另一張紙的最后一行。
之所以這樣做,是因為我想將數據合並到一個已經存在並且包含了數據的工作表中。
到目前為止,以下是我的代碼,該代碼僅再次復制到另一張紙的A2中。 為此,我應該采取什么方法:
Sub Upload()
Dim Wb1 As Workbook
Dim Wb2 As Workbook
Dim MainPage As Worksheet
Set MainPage = Sheets("Main")
Dim r As Long
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set Wb1 = ActiveWorkbook
FileToOpen = Application.GetOpenFilename _
(Title:="Please choose a File", _
filefilter:="Excel File *.xlsx (*.xlsx),")
If FileToOpen = False Then
MsgBox "No File Specified.", vbExclamation, "ERROR"
Exit Sub
Else
Set Wb2 = Workbooks.Open(Filename:=FileToOpen)
With Wb2.Sheets("ALL TICKETS (excpt Open-OnHold)")
srcLastRow = .Range("A:AJ").Find("*", SearchOrder:=xlByRows,
SearchDirection:=xlPrevious).Row
destLastRow = Wb1.Sheets("ALL TICKETS (excpt Open-OnHold)".Range("A:AJ").Find("*", SearchOrder:=xlByRows,
SearchDirection:=xlPrevious).Row + 1
Wb1.Sheets("ALL TICKETS (excpt Open-OnHold)").Range("A2:AJ" &
destLastRow).Value = .Range("A2", "AJ" & srcLastRow).Value
End With
Wb2.Close
End If
End Sub
您知道復制的范圍,因此您需要知道目標表的最后一行:
dim lr as long
With Sheets("Destination")
lr = .cells(.rows.count,1).end(xlup).row 'assumes column 1 is contiguous
End with
然后,您可以使用源范圍(將使用變量SrcRng)並將其粘貼到新工作表的特定單元格中:
SrcRng.Copy Sheets("Destination").Cells(lr+1,1) 'this line does the copy and the paste
復制的范圍的其余部分將被填寫。
編輯1:
很難在注釋中顯示代碼...
Dim LRSrc as Long, LRDest as Long, SrcRng as Range
With Sheets("Source")
LRSrc = .cells(.rows.count,1).end(xlup).row 'assumes column 1 is contiguous
Set SrcRng = .Range("A1:AJ" & LRSrc)
End with
With Sheets("Destination")
LRDest = .cells(.rows.count,1).end(xlup).row 'assumes column 1 is contiguous
SrcRng.Copy .Cells(LRDest+1,1)
End with
這會為您工作。 如下定義srcLastRow。
srcLastRow =單元格(行數,36)。結束(xlUp)。行
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.