简体   繁体   English

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

[英]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.

相关问题 VBA代码可从一个工作表复制数据并将其粘贴到另一工作表的最后一行下方 - VBA code to copy data from one worksheet & paste below last row of another worksheet Excel VBA,将数据从一个工作表复制并粘贴到另一个工作表,然后删除复制数据源 - Excel VBA, Copy and paste data from one worksheet to another then delete copy data source 在一个工作表中复制一行并粘贴到另一个工作表中 - copy a row in one worksheet and paste in another worksheet VBA Excel脚本从一个工作表复制包含数据的行并粘贴到另一个工作表中而不会覆盖 - VBA Excel Script to copy rows with data from one worksheet & paste into another with out overwritting 遍历行复制并粘贴到VBA Excel中的另一个工作表 - loop through row copy and paste to another worksheet in VBA Excel Excel VBA-通过循环将数据从一个工作表复制到另一个工作表 - Excel VBA - Copy data from one worksheet to another via loop Excel使用VBA将数据从一张纸复制到工作表上的另一张 - Excel copy data from one sheet to another on worksheet refresh with vba Excel/VBA - 根据日期将数据复制并粘贴到特定行中的工作表中 - Excel/VBA - Copy and paste data into a worksheet in a specific row based on date Excel vba将来自不同工作表的单元格复制并粘贴到新工作表 - Excel vba to copy and paste a cells from different worksheets to new worksheet 将行从一个行复制并粘贴到另一个工作表VBA - Copy and paste rows from one to another worksheet VBA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM