在Excel中使用vba第一个空单元格之后如何正确地将数据从一个工作表复制到另一个工作表?

时间:2022-11-20 07:40:33

I have a button on my sheet that when clicked is supposed to copy data from my "template" sheet below the first blank cell. Unfortunately it instead creates a whole new workbook with the copied data. I have it set to copy the template data when a new sheet is created that works as expected but for some reason this button does not. Here is my code:

我的工作表上有一个按钮,单击该按钮时,应该从第一个空白单元格下方的“模板”表中复制数据。不幸的是,它改为创建一个包含复制数据的全新工作簿。我设置为在创建按预期工作的新工作表时复制模板数据,但由于某种原因,此按钮不会。这是我的代码:

Sub paste_newcalc()
    Set WshSrc = ThisWorkbook.Worksheets("Template")

    Dim rFirstBlank As Range

    ThisWorkbook.Activate

    WshSrc.Copy

    Set rFirstBlank = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Offset(1)

    With ActiveSheet.Cells
        .PasteSpecial

    End With

End Sub

What I have used there is what I have found through searching the internet for a solution.

我在那里使用的是通过在互联网上寻找解决方案而找到的东西。

2 个解决方案

#1


1  

Ok. With the help of the comments above, I altered my vba code and found a solution. If I have a default range of data on one sheet, let's call it "Template", and I want to copy that below the data I already have in my current sheet with the click of a button, the following code will do it.

好。在上述评论的帮助下,我修改了我的vba代码并找到了解决方案。如果我在一张纸上有一个默认的数据范围,我们称之为“模板”,并且我想在我当前工作表中已经拥有的数据下面复制一下按钮,下面的代码就可以了。

Sub paste_newcalc()
    Dim rFirstBlank As Range

    Set rFirstBlank = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Offset(1)

    Worksheets("Template").Range("A4:D40").Copy

    rFirstBlank.PasteSpecial


End Sub

That will paste the range you want from another sheet below the data you already have in the first empty cell. Thank you to those that commented!

这将从您在第一个空单元格中已有的数据下方的另一个工作表中粘贴所需的范围。谢谢那些评论的人!

#2


0  

Sub paste_newcalc()
     Dim rFirstBlank As Range
     Set rFirstBlank = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Offset(1)
     Worksheets("Template").Range("A4:D40").Copy rFirstBlank
End Sub

#1


1  

Ok. With the help of the comments above, I altered my vba code and found a solution. If I have a default range of data on one sheet, let's call it "Template", and I want to copy that below the data I already have in my current sheet with the click of a button, the following code will do it.

好。在上述评论的帮助下,我修改了我的vba代码并找到了解决方案。如果我在一张纸上有一个默认的数据范围,我们称之为“模板”,并且我想在我当前工作表中已经拥有的数据下面复制一下按钮,下面的代码就可以了。

Sub paste_newcalc()
    Dim rFirstBlank As Range

    Set rFirstBlank = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Offset(1)

    Worksheets("Template").Range("A4:D40").Copy

    rFirstBlank.PasteSpecial


End Sub

That will paste the range you want from another sheet below the data you already have in the first empty cell. Thank you to those that commented!

这将从您在第一个空单元格中已有的数据下方的另一个工作表中粘贴所需的范围。谢谢那些评论的人!

#2


0  

Sub paste_newcalc()
     Dim rFirstBlank As Range
     Set rFirstBlank = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Offset(1)
     Worksheets("Template").Range("A4:D40").Copy rFirstBlank
End Sub