Excel-VBA将数据从一个工作表复制到另一个工作表并粘贴到新行中

时间:2018-05-15 07:32:54

标签: excel-vba vba excel

我是VBA的新手。以下是我的代码。我想要做的是从一个工作表中复制数据并将其粘贴到另一个工作表中。我的代码唯一的问题是,每次我尝试复制一个新文件时,它都会覆盖以前的数据。我想要的是将数据粘贴到新行。

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

1 个答案:

答案 0 :(得分:0)

下一行由

给出
Set PasteStart = PasteStart.Offset(1,0)   '1 row down, no columns

表单中的下一个空白行(我怀疑是你想要的)由

给出
set pastestart = worksheets("Dominoes_Excel").cells(worksheets("Dominoes_Excel").rows.count,1).end(xlup).offset(1,0)