简体   繁体   English

Excel VBA-将范围从一张纸复制到另一张纸,下一个空行

[英]Excel VBA - Copy range from one sheet to another, next empty row

I'm trying to take a range from one sheet and copy it to the next empty row in another sheet (basically, paste into the range A12:D12 for next empty row in the other sheet). 我正在尝试从一个工作表获取一个范围,并将其复制到另一工作表中的下一个空行(基本上,将其粘贴到另一工作表中下一个空行的范围A12:D12中)。 The range will never change. 范围永远不会改变。 I've seen a lot of questions like this, with people saying the answers work great, but I can't get it to work. 我已经看到很多类似的问题,有人说答案很有效,但我无法解决。

Very new to VBA. VBA的新手。 Here is the code I'm using: 这是我正在使用的代码:

Private Sub CommandButton1_Click()

    Dim NextRow As Range
    Set NextRow = Range("A" & Sheets("Sheet3").UsedRange.Rows.Count + 1)  

    Sheet1.Range("A12:D12").Copy
    Sheet3.Activate
    NextRow.PasteSpecial Paste:=xlValues, Transpose:=False
    Application.CutCopyMode = False

    Set NextRow = Nothing

End Sub

This runs but it doesn't actually paste any values into Sheet3 . 这会运行,但实际上不会将任何值粘贴到Sheet3 Is there something I'm missing? 有什么我想念的吗? Is there a better way to do this? 有一个更好的方法吗?

Thanks! 谢谢!

You just had an issue in the second line defining NextRow . 您在定义NextRow的第二行中遇到了一个问题。
Is there a better way to do this? 有一个更好的方法吗? It depends on your needs; 这取决于您的需求; personally I do not like to activate/select other cells during a VBA macro so eg I would get rid of the Sheet3.Activate . 我个人不喜欢在VBA宏中激活/选择其他单元格,因此例如,我将摆脱Sheet3.Activate I would also copy the stuff 'manually' without using the clipboard to avoid changing the user's clipboard contents. 我也将“手动”复制内容而不使用剪贴板,以避免更改用户的剪贴板内容。

Private Sub CommandButton1_Click()

    Dim NextRow As Range
    Set NextRow = Sheet3.Range("A" & Sheet3.Rows.Count).End(xlUp).Offset(1, 0)

    Sheet1.Range("A12:D12").Copy
    Sheet3.Activate
    NextRow.PasteSpecial Paste:=xlValues, Transpose:=False
    Application.CutCopyMode = False

    Set NextRow = Nothing

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用VBA excel从一张工作表中复制表格并将其粘贴到另一张工作表的下一个空行中 - copy a table from one Sheet and paste it in the next empty row of another Sheet using VBA excel Excel 2007 VBA:如何从一张纸上的动态范围复制和粘贴到另一张纸的第一行? - Excel 2007 VBA: How do I copy and paste from a dynamic range on one sheet to the first empty row of another sheet? 从范围复制并过去到下一个空单元格中的另一个工作表中 - Copy from a range and past in another sheet in the next empty cell in a row Excel VBA将行范围从一个工作表复制到另一工作表的特定行 - Excel VBA to copy a row range from one sheet to a particular row in another 从一张纸上复制一定范围的单元格,然后插入下一个空行 - Copy a range of cells from one sheet and insert into next empty row excel vba 范围从一张纸复制到另一张纸(错误 1004) - excel vba range copy from one sheet to another (error 1004) 将数据从一张纸复制到下一个空行的另一张纸上 - copy data from one sheet to another on the next empty row 来自另一张工作表的Excel VBA范围副本 - Excel VBA range copy from another sheet 复制列直到一张纸的最后一行,然后粘贴到另一张纸的下一个空行 - Copy columns until last row from one sheet and paste to the next empty row of another sheet Excel VBA 从一个工作表中复制范围并将其一次一个单元格地粘贴到另一张工作表中 - Excel VBA copy range from one sheet and paste it one cell at a time in another sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM