vba 插入特定行数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43525799/
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
Insert Specific Number of Rows
提问by user6888830
I'm trying to insert a specific number of rows. Right now I'm using the below code to insert 4 lines. I'm trying to write a line of code that will insert a certain number or rows based on a number in a certain cell. For example, if I wanted to insert 4 rows and cell A2 is the cell where I can change the number of rows I want to add, what code would I use to add any number of rows based off what number I insert in cell A2.
我正在尝试插入特定数量的行。现在我正在使用下面的代码插入 4 行。我正在尝试编写一行代码,根据特定单元格中的数字插入特定数字或行。例如,如果我想插入 4 行,而单元格 A2 是我可以更改要添加的行数的单元格,我将使用什么代码根据我在单元格 A2 中插入的数字来添加任意数量的行。
ActiveCell.EntireRow.Select
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
回答by Shai Rado
You can use something like the line below to insert 4 rows at once:
您可以使用类似于以下行的内容一次插入 4 行:
ActiveCell.EntireRow.Resize(4).Insert Shift:=xlDown
or maybe the line below (depends on where you want the added rows to be added):
或者可能是下面的行(取决于您希望添加行的位置):
ActiveCell.EntireRow.Offset(1).Resize(4).Insert Shift:=xlDown
and without the need to use ActiveCell
, which is always recommended:
并且无需使用ActiveCell
,始终建议使用:
Range("A2").EntireRow.Offset(1).Resize(4).Insert Shift:=xlDown
回答by pascalb
This should work
这应该工作
Sub InsertRow() Dim ws As Worksheet Dim NBOFROWS As Range Set ws = ThisWorkbook.ActiveSheet With ws Set NBOFROWS = .Range("A2") ActiveCell.EntireRow.Offset(1).Resize(NBOFROWS.Value).Insert Shift:=xlDown End With End Sub