将数据从一个工作表复制到另一个工作表的最后一行

时间:2018-03-15 15:13:40

标签: excel vba excel-vba

我正在尝试将数据从一张纸复制到另一张纸的最后一行。

我这样做的原因是因为我想合并已经存在的工作表中的数据并且我已经包含了数据。

以下是我的代码到目前为止只复制到另一张表的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

2 个答案:

答案 0 :(得分:1)

您知道复制的范围,因此您需要知道目标表的最后一行:

RowID   ID1     ID2     ID3     Word1       Word2       Word3       Letter  LastLetterWord2
1       1       2       3       the         nice        apple       t       e   
2       1       2       3       the         nice        apple       h       e
3       1       2       3       the         nice        apple       e       e
4       1       2       3       the         nice        apple       n       e
5       1       2       3       the         nice        apple       i       e
6       1       2       3       the         nice        apple       c       e
7       1       2       3       the         nice        apple       e       e
8       1       2       3       the         nice        apple       a       e
9       1       2       3       the         nice        apple       p       e
10      1       2       3       the         nice        apple       p       e
11      1       2       3       the         nice        apple       l       e
12      1       2       3       the         nice        apple       e       e
13      6       7       8       yes         did         you         y       d
14      6       7       8       yes         did         you         e       d
15      6       7       8       yes         did         you         s       d
16      6       7       8       yes         did         you         d       d
17      6       7       8       yes         did         you         i       d
18      6       7       8       yes         did         you         d       d
19      6       7       8       yes         did         you         y       d
20      6       7       8       yes         did         you         o       d
21      6       7       8       yes         did         you         u       d

然后,您可以获取源范围(将使用变量SrcRng)并粘贴到新工作表,进入特定单元格:

dim lr as long
With Sheets("Destination")
    lr = .cells(.rows.count,1).end(xlup).row 'assumes column 1 is contiguous
End with

将填写复制范围的其余部分。

EDIT1:

难以在评论中显示代码......

SrcRng.Copy Sheets("Destination").Cells(lr+1,1) 'this line does the copy and the paste

答案 1 :(得分:0)

这对你有用吗? 定义srcLastRow如下。

srcLastRow = Cells(Rows.Count,36).End(xlUp).Row