[英]Excel-VBA Copy data from one worksheet to another worksheet and paste in new row
I am new to VBA. 我是VBA的新手。 And below is my code.What i am trying to do is copy data from one worksheet and paste it in another worksheet. 下面是我的代码。我想做的是从一个工作表中复制数据并将其粘贴到另一个工作表中。 The only problem with my code is that every time i try to copy a new file it overwrites the previous data . 我的代码唯一的问题是,每次我尝试复制一个新文件时,它都会覆盖以前的数据。 What i want is to paste data in new line. 我想要的是将数据粘贴到新行中。
Private Sub CommandButton1_Click()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim Sheet As Worksheet
Dim PasteStart As Range
Dim erow
Set wb1 = ActiveWorkbook
Set PasteStart = [Dominoes_Excel!A1]
FileToOpen = Application.GetOpenFilename
If FileToOpen = False Then
MsgBox ("No File Specified.")
Exit Sub
Else
Set wb2 = Workbooks.Open(Filename:=FileToOpen)
For Each Sheet In wb2.Sheets
With Sheet.UsedRange
.Copy PasteStart
Set PasteStart = PasteStart.Offset(.Rows.Count)
End With
Next Sheet
End If
wb2.Close
End Sub
The next line down is given by 下一行由
Set PasteStart = PasteStart.Offset(1,0) '1 row down, no columns
The next blank line in a sheet (which I suspect is what you want) is given by 工作表中的下一个空白行(我怀疑是您想要的)由
set pastestart = worksheets("Dominoes_Excel").cells(worksheets("Dominoes_Excel").rows.count,1).end(xlup).offset(1,0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.