在Excel中, 右键-插入(删除),我们就能快速插入和删除行了。那么如何在批量插入和删除空行呢下面我们用Excel VBA快速对几种插入删除空行方式处理一下
1、每隔n行插入空行Sub insertBlankRow()
Dim rowsn As IntegerDim n As IntegerDim k As Longn = 1 '如果想每隔3行插入1行,此处更改为n=3rowsn = Int(Range("B" & Cells.rows.Count).End(xlUp).Row / n)For I = 1 To rowsnrows(I * n + 1 + k).Insertk = k + 1Next IEnd Sub
2、删除空行(全部单元格为空的空行)Sub DeleteBlankRow()Dim firstRow As LongDim LastRow As LongDim i As LongfirstRow = ActiveSheet.UsedRange.RowLastRow = firstRow + ActiveSheet.UsedRange.Rows.Count - 1For i = LastRow To firstRow Step -1If Application.WorksheetFunction.CountA(Rows(i)) = 0 ThenRows(i).DeleteEnd IfNext iEnd Sub
3、删除空行(以某列为基准,某行在此列为空即删除)Sub deleteBlankRowOfColumn()Columns(1).SpecialCells(xlCellTypeBlanks).EntireRow.DeleteEnd Sub
如下图,在DeleteBlankRow()过程中,必须是整行为空,该行才删除。而过程deleteBlankRowOfColumn()是以第一列为基础,该列中为空的单元格整行删除。(下图第7行在deleteBlankRowOfColumn()会被删,而DeleteBlankRow()过程不会) 1498814127786104.jpg(49.77 KB, 下载次数: 0, 售价: 4 金钱)