简体   繁体   English

借助VBA宏将预制模板移动/插入到新范围

[英]assistance with VBA macro to move / insert premade template into a new range

I am currently working on a excel-VBA macro that would selected a template, copy it, create extra rows above the selected cell and afterwards paste the template into the added extra rows. 我目前正在研究excel-VBA宏,该宏将选择模板,将其复制,在所选单元格上方创建额外的行,然后将模板粘贴到添加的额外行中。 So far my code looks like this: 到目前为止,我的代码如下所示:

Sub Insert_Another_Good()
    Dim i As Integer
    Dim j As Integer
    ActiveCell.EntireRow.Select
    i = 8
    For j = 1 To i
        Selection.Insert Shift:=xlToDown, CopyOrigin:=xlFormatFromRightorAbove
    Next: Macro2
    Range("A25:P32").Copy
    Set rng = Range(Selection.Address)
    ActiveSheet.Paste
End Sub

Sub Macro2() 'still part of the same sub'
    Range("A25:P32").Copy
    Set rng = Range(Selection.Address)
    ActiveSheet.Paste
End Sub

This works well for copying the template and pasting it into the new lines but it only pastes starting from the very first, A, column. 这对于复制模板并将其粘贴到新行中非常有效,但是它仅从第一列A开始粘贴。 Could you please help me make it so it pastes the template in lets say column Q? 您能否帮我做到这一点,以便将模板粘贴到“ Q”列中? (Bus still the same row as the first line out of new 8 posted) (公交车仍与新发布的8条中的第一行相同)

Hope you understood my weird question ! 希望你理解我的奇怪问题! And thanks for help in advance! 在此先感谢您的帮助!

How about instead of inserting empty rows you simply insert the copied range and shift everything down, like so: 而不是插入空行,您只需插入复制的范围并向下移动所有内容,就像这样:

Range("A25:P32").Copy
Set Rng = Range(Selection.Address)
Selection.Insert Shift:=xlDown

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

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