我有一张包含多个重复行块的工作表。像这样:
SALAD
Small
Medium
Large
FRIES
Small
Medium
Large
BURGERS
Small
Medium
Large
如何在中间添加10行" Medium"和"大"在每个部分?
我的工作表有数百种这类部分,所以我不能手动完成。
如果你想只添加一行,我可以使用控件+ F,搜索"大,"选择全部,然后添加一行。但是我每次要添加的每一行都要这样做,总共10次。
谢谢!
答案 0 :(得分:1)
您可以使用VBA轻松完成此操作。要进入VBA编辑器,请在Excel中按ALT + F11。创建一个新模块(Insert> Module)并粘贴以下代码:
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, e.g. 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或单击绿色播放按钮。
我已经对代码(所有以撇号开头的行)进行了评论以供解释。此代码在列中循环,当单元格值为“大”时,插入3行。当然,在您的示例中,将其更改为10,然后您可以将“大”更改为您希望的任何内容。
结果如下:
希望这有帮助