如何在大型电子表格中每三个单元插入一行

希望你能帮助一个基本的Excel用户!

我有一个约2000行的文件,我需要添加一个行/中断后每三分之一。 请问有这样一个简单的方法吗?

您的帮助和build议将不胜感激。

谢谢

没有VBA的快速方法

  1. 在空列中join这个公式, =IF(MOD(ROW(),3)=0,NA(),"")
  2. 按F5, Goto …. SpecialFormulas Errors (每隔Errorsselect一次)
  3. 插入行

步骤2如下所示

在这里输入图像说明

如果你想尝试一些VBA这里是一个button单击事件,将做插入每三行。 如果您有任何问题,请告诉我。

 Private Sub CommandButton1_Click() Dim ws As Excel.Worksheet Dim lRow As Long Dim lastRow As Long 'Set the worksheet object to the sheet by name Set ws = Application.Sheets("Sheet1") 'Set the row to start looping(inserting) rows at lRow = 4 'Find the last row with a value in column A lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row 'Account for the amount of rows that will be inserted. lastRow = lastRow + (lastRow * 0.33) 'Loop through the worksheet from the start row to the last row Do While lRow <= lastRow 'Insert a row ws.Rows(lRow).EntireRow.Insert 'Increment the row to insert at on the next pass of the loop lRow = lRow + 4 Loop End Sub 

一个非VB的方式是创build一个新的列和插入编号

 1 2 3 4 5 6 ...... 

然后为空行,编号为3,6,9,….(假设你有2Klogging,重复它2K),然后按数字列sorting,然后删除列

在这里输入图像说明 在这里输入图像说明

如果您的内容相同或重复,则可以在任何基本文本编辑器中打开该文件,然后执行以下操作:

  1. 突出显示并复制前三行(包括第三个换行符)。
  2. 使用find-and-replace(Mac上的TextEdit中的Opt-Cmd-F),将该内容复制到“查找”字段以及“replace”字段。
  3. 在“replace”字段中粘贴的内容末尾添加换行符。
  4. 执行查找和replace操作。

这应该是这样的:

 item item item item item item item 

…进入这个:

 item item item item item item item 

…等等。

这绝对不是最优雅的解决scheme,但它是最快/最简单的,我看到没有诉诸在bash中的文本parsing脚本等

假设数据在A1中开始,并在B1中复制并适合(即在ColumnA中填充单元格的末尾):

 =IF(MOD(ROW(),4)=0,"",OFFSET(A$1,3*INT((ROW()-1)/4)+MOD(ROW(),4)-1,)) 

这是一个相当于Eric K.上面提出的解决scheme的VBA。 方向假定行1中的列标题标签应该单独保留。

 Sub insBlankFourthRow() Debug.Print Timer With Worksheets("Sheet3") .Columns(1).Insert With .Cells(1, 1).CurrentRegion '<~~ original CurrentRegion With .Resize(.Rows.Count - 1, 1).Offset(1, 0) .Cells(1, 1) = 1 .Cells.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Step:=1 End With With .Resize(Int(.Rows.Count / 3) + 1, 1).Offset(.Rows.Count, 0) .Cells(1, 1) = 3.5 .Cells.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Step:=3 End With End With ' With .Cells(1, 1).CurrentRegion '<~~ new expanded CurrentRegion .Cells.Sort Key1:=.Columns(1), Order1:=xlAscending, _ Orientation:=xlTopToBottom, Header:=xlYes End With .Columns(1).Delete End With Debug.Print Timer End Sub 

tbh,〜2000行的数据并不是那么值得担心,但是10×或100×的数据量在单行插入或大块非连续方向插入时会开始显着滞后。 一个Range.DataSeries方法 (我知道填充序列的最快方式)填充的“帮助”列可以很容易地被丢弃,一旦其目的已经实现。

insert_blank_fourth_row

对2500张典型的图像随机数据进行上述处理,每秒钟的⁸/ 100秒。 通过禁用Application.ScreenUpdating属性和类似的开销,这个时间可能会适度地得到改善。