从相同单元格值VBA开始,在Excel中添加多行

我有一个有多个重复行的块。 像这样的东西:

SALAD Small Medium Large FRIES Small Medium Large BURGERS Small Medium Large 

我怎样才能在每个部分的“中”和“大”之间添加10行?

我的工作表有几百个这样的部分,所以我不能手动完成。

如果你只想添加一行,我可以使用控制+ F,search“大”,select全部,并添加一行。 但是我每次都要为每一行添加10次。

谢谢!

你可以用VBA轻松做到这一点。 要进入VBA编辑器,在Excel中按ALT + F11。 创build一个新的模块(插入>模块)并粘贴以下代码:

 Sub insertRows() Dim vcell As Range Dim i As Integer Dim j As Integer Dim lastRow As Integer ' Use WITH for shorthand (everything starting ".") ' because all cell references are to current sheet With ThisWorkbook.ActiveSheet ' Cycle through each cell in the used range of column 1, ' start by getting the last row lastRow = .UsedRange.Rows.Count ' Go bottom to top as inserting new rows pushes everything down For i = lastRow To 1 Step -1 ' Set cell as row i, column 1 Set vcell = .Cells(i, 1) ' If it is a cell with value LARGE then do some action If vcell.Value = "Large" Then ' loop for desired number of times, eg 3 For j = 1 To 3 ' Insert a new row above VCELL vcell.EntireRow.Insert Next j End If Next i End With End Sub 

要运行代码按F5或单击绿色播放button。

我已经评论了代码(所有以撇号开头的行)来解释。 此代码在列中循环,当单元格值为“大”时,插入3行。 当然在你的例子中,把它改为10,稍后你可以把“Large”改成你想要的任何东西。

以下是结果:

之前和之后的列

希望这可以帮助