简体   繁体   中英

Excel-VBA Copy data from one worksheet to another worksheet and paste in new row

I am new to 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)

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