vba 如何在特定行后插入新的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20626100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 00:59:04  来源:igfitidea点击:

How to Insert A New Between After a Specific Row

vbaexcel-vbaexcel-2007excel

提问by Behseini

Can you please let me know how I can
1- insert a new row let say 3 exactly after Existing row 2
2- Copy Strings from 1 to 2
3- and finally delete 1

您能否让我知道如何
1- 在现有行 2 之后准确插入一个新行,例如 3
2- 将字符串从 1 复制到 2
3- 最后删除 1

The reason that i have to do this is I have some Drop-down, look ups(which I do not know what they call in excel) in row 1 and i can not load the file like this into GIS software so I need to get rid of that, however I still need to keep the Title headers for further referencing.

我必须这样做的原因是我在第 1 行有一些下拉菜单(我不知道他们在 excel 中叫什么),我无法将这样的文件加载到 GIS 软件中,所以我需要获取摆脱它,但是我仍然需要保留标题标题以供进一步参考。

thanks Update

谢谢 更新

Sub inserter()
'
' inserter Macro
'

'
    Rows("3:3").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Rows("2:2").Select
    Selection.Copy
    Rows("3:3").Select
    ActiveSheet.Paste
    Rows("2:2").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlUp
End Sub

回答by bto.rdz

Check this code:

检查此代码:

Rows("3:3").Select
Selection.Delete Shift:=xlUp
Range("B4").Select
ActiveCell.FormulaR1C1 = "hello"
Range("B4").Select
Selection.Copy
Range("C4").Select
ActiveSheet.Paste
Rows("4:4").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp

but better learn to save macros. I mean I dont have in my head this code, everytime I need some code I just save it, btw avoid .select, do something like this instead:

但最好学会保存宏。我的意思是我脑子里没有这个代码,每次我需要一些代码时我都会保存它,顺便说一句,避免使用 .select,而是做这样的事情:

Rows("3:3").Delete Shift:=xlUp

Hope it helps

希望能帮助到你

回答by Siddharth Rout

Um not exactly. If you notice this line

嗯不完全是。如果你注意到这条线

.Rows(1).Copy .Rows(2)

It follows what you had in point 2 and it not as per what you recorded.

它遵循您在第 2 点中的内容,而不是您记录的内容。

Macro recorder gives all kinds of extra code. The code that you gave can be written as

宏记录器提供了各种额外的代码。你给的代码可以写成

Private Sub Sample()
    Dim ws As Worksheet

    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        .Rows(3).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        .Rows(1).Copy .Rows(2)
        .Rows(1).Delete Shift:=xlUp
    End With
End Sub

You might also want to see THIS

你可能还想看这个