简体   繁体   English

VBA复制并向下粘贴2行

[英]VBA Copy and Pasting 2 Rows Down

I've only just come across VBA so I'm a complete novice. 我只是接触过VBA,所以我是一个完整的新手。 Essentially I'm currently automating a form that requires writing out the questions, potential answers, instructions, etc. for developers on a spreadsheet. 基本上,我目前正在自动化一个表单,该表单要求在电子表格上为开发人员写出问题,可能的答案,说明等。 I've created a basic template table so all the questions are structured the same. 我创建了一个基本模板表,因此所有问题的结构都相同。 I want to copy and paste this table (clearing the contents and taking off the number of the question) and paste it 2 rows down from the bottom of the last table. 我想复制并粘贴此表(清除内容并删除问题的编号),然后将其从最后一张表的底部向下两行粘贴。

The code works fine if I just wanted to copy and paste the table directly below the first but I can't go any further than that. 如果我只想将表复制并粘贴到第一个表的正下方,则代码可以正常工作,但是我不能做得更多。 I'm not sure how to write that I want it to find the last filled in row and paste the table 2 rows below. 我不确定该如何写,是否要查找最后填写的行并将表粘贴到下面的2行中。

Sub Macro2()
'
' Macro2 Macro
'
' Keyboard Shortcut: Ctrl+g
'
    Range("C2:G6").Select
    Selection.ClearContents

    Range("A2").Select
    ActiveCell.FormulaR1C1 = "C"

    Range("A2:G6").Select
    Selection.Copy

    Range("A8").Select
    ActiveSheet.Paste
End Sub

You can completely avoid using select in order to achieve your goal. 您可以完全避免使用select来实现您的目标。 In the following code, Source is the range of your table, LastRow finds the last row of your table and DestRng is the destination where you want a copy of your table. 在下面的代码中, Source是表的范围, LastRow查找表的最后一行, DestRng是要复制表的目标位置。 Hope this helps! 希望这可以帮助!

Sub Macro2()
'
' Macro2 Macro
'
' Keyboard Shortcut: Ctrl+g
'
    Dim TblRng As Range
    Set TblRng = Range("C2:G6")
    TblRng.ClearContents

    Dim Source As Range
    Set Source = Worksheets("Sheet2").Range(("A2"), Range("G2").End(xlDown))
    Source.Copy

    Dim LastRow As Long
    LastRow = Cells(Rows.Count, "G").End(xlUp).Row

    Dim DestRng As Range
    Set DestRng = Source(LastRow + 1, "A")
    DestRng.PasteSpecial xlPasteAll

End Sub

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM