如何用VBA批量删除Excel工作簿所有工作表C列空值所在行
适配全工作簿遍历的优化VBA代码
原代码仅支持当前活动工作表,且冗余的Select操作会降低运行效率,优化后代码如下:
Sub DelAllBlankRowsInColC() Dim ws As Worksheet ' 关闭屏幕更新大幅提升批量处理速度 Application.ScreenUpdating = False For Each ws In ThisWorkbook.Worksheets ' 跳过隐藏工作表可删除下行注释 ' If ws.Visible = xlSheetVisible Then On Error Resume Next ' 直接定位C列空单元格并删除整行,无需选中操作 ws.Columns("C:C").SpecialCells(xlCellTypeBlanks).EntireRow.Delete On Error GoTo 0 ' End If Next ws Application.ScreenUpdating = True MsgBox "全工作簿C列空行清理完成" End Sub
额外说明
- 运行代码前务必做好原文件备份,防止误删数据无法回溯
- 上述代码仅识别真正的空单元格,如果你的表格中存在公式返回的空文本
"",这类值不会被SpecialCells(xlCellTypeBlanks)识别,可改用筛选逻辑实现:
Sub DelEmptyRowsIncludeFormulaBlank() Dim ws As Worksheet, lastRow As Long Application.ScreenUpdating = False For Each ws In ThisWorkbook.Worksheets lastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row If lastRow > 1 Then ' 排除表头只有1行的情况 With ws.Range("C1:C" & lastRow) .AutoFilter Field:=1, Criteria1:="=" .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete End With ws.AutoFilterMode = False End If Next ws Application.ScreenUpdating = True MsgBox "全工作簿C列空值(含公式空文本)清理完成" End Sub
- 如果需要跳过隐藏工作表,取消第一段代码中对应注释的注释符号即可
内容的提问来源于stack exchange,提问作者garrmartin
相关产品推荐
相关产品推荐

