跳至主要内容

在 Excel 中删除空行的 6 种简单方法(一步步指导)

Author: Sun Last Modified: 2025-05-07

当您处理包含空行的大型数据集时,这些空行可能会使您的工作表变得杂乱,并阻碍数据分析。虽然您可以手动删除少量的空行,但在处理数百个空行时,这种方法会变得耗时且效率低下。在本教程中,我们将介绍六种不同的方法,帮助您批量高效地删除空行。这些技术涵盖了您在 Excel 中可能遇到的各种场景,让您可以使用更整洁、更有条理的数据进行工作。

A screenshot showing an Excel worksheet with blank rows for removal


视频:删除空行


删除空行

 

在从数据集中删除空行时,务必小心谨慎,因为一些常见的方法可能会意外删除包含数据的行。例如,互联网上流行的两个技巧(本教程后面也会提到)是:

  • 使用“定位条件”选择空白单元格,然后删除这些选中空白单元格所在的行。
  • 使用筛选功能在关键列中筛选出空白单元格,然后删除筛选范围中的空行。

然而,这两种方法都有可能错误地删除包含重要数据的行,如下截图所示。

A screenshot showing a dataset in Excel where careless removal of blank rows could delete important data

为避免这种意外删除,建议使用以下四种方法之一来精确删除空行。


>> 通过辅助列删除空行

步骤 1:添加辅助列并使用 COUNTA 函数
  1. 在数据集的最右侧,添加“辅助”列,并在该列的第一个单元格中输入以下公式:
    =COUNTA(A2:C2)
    A screenshot showing the addition of a helper column and the COUNTA function to identify blank rows in Excel
    注意:在公式中,A2:C2 是您要统计非空单元格的区域。
  2. 然后向下拖动自动填充柄以填充公式,统计每一行中非空单元格的数量。“0”表示相对应的行完全为空。
    A screenshot showing the COUNTA formula applied across rows to detect blank rows in Excel
步骤 2:通过辅助列筛选空行
  1. 点击辅助列中的任意单元格,选择“数据”>“筛选”。
    A screenshot showing the Filter option in Excel ribbon
  2. 然后点击“筛选箭头”,在展开的菜单中仅勾选“0”,然后点击“确定”。
    A screenshot showing the use of the Filter feature to filter out blank rows on the helper column

现在所有空行都已被筛选出来。

A screenshot showing blank rows filtered out using the helper column in Excel

步骤 3:删除空行

选择空行(点击行号并向下拖动以选择所有空行),然后右键单击,在上下文菜单中选择“删除行”(或者可以使用快捷键“Ctrl” + “-”)。

A screenshot showing the deletion of blank rows using the context menu in Excel

步骤 4:在排序和筛选组中选择筛选以清除已应用的筛选

A screenshot showing the Filter option in Excel ribbon to clear the applied filter in Excel

结果:

A screenshot showing the result of removing blank rows in Excel after using the helper column method

注意:如果不再需要辅助列,请在筛选后将其删除。

>> 使用 Kutools 在 3 秒内删除空行

为了快速且毫不费力地准确消除所选内容中的空行,最佳解决方案是利用“Kutools for Excel”的“删除空行”功能。以下是具体操作方法:

Kutools for Excel 提供了超过 300 种高级功能,简化复杂任务,提升创造力与效率。 通过集成 AI 能力,Kutools 能够精准自动执行任务,让数据管理变得轻松简单。Kutools for Excel 的详细信息...         免费试用...
  1. 选择要删除空行的区域。
  2. 点击“Kutools”>“删除”>“删除空行”>“选择区域”。
  3. 根据需要选择所需的选项,并在弹出的对话框中点击“确定”。

    A screenshot showing how to use Kutools to delete blank rows in Excel from a selected range

附加信息:
  • 除了删除选定区域内的空行外,“Kutools for Excel”还可以让您通过单击快速删除“当前工作表”、“选中的工作表”或“整个工作簿”中的空行。

  • 在使用删除空行功能之前,请先安装 Kutools for Excel。点击这里立即下载 Kutools for Excel


>> 手动删除空行

如果只有少量的空行需要删除,也可以手动删除它们。

步骤 1:选择空行

点击行号以选择单个空行。如果有多个空行,按住“Ctrl”键并逐一点击行号以选择它们。

A screenshot showing how to select blank rows manually in Excel using the row numbers

步骤 2:删除空行

选择空行后,右键单击并在上下文菜单中选择“删除”(或者可以使用快捷键“Ctrl” + “-”)。

A screenshot showing the context menu to delete selected blank rows in Excel

结果:

A screenshot showing the result after manually removing blank rows in Excel


>> 使用 VBA 删除空行

如果您对 VBA 感兴趣,本教程提供了两种 VBA 代码供您从选定区域和活动工作表中删除空行。

步骤 1:将 VBA 复制到 Microsoft Visual Basic for Applications 窗口中
  1. 激活要删除空行的工作表,然后按下“Alt” + “F11”键。

    A screenshot showing the shortcut key Alt + F11

  2. 在弹出的窗口中,点击“插入”>“模块”。

  3. 然后复制并将以下代码之一粘贴到新的空白模块中。

    代码 1:从活动工作表中删除空行

    Sub RemoveBlankRows()
    'UpdatebyExtendoffice
        Dim wsheet As Worksheet
        Dim lastRow As Long
        Dim i As Long
        
        ' Set the worksheet variable to the active sheet
        Set wsheet = ActiveSheet
        
        ' Get the last row of data in the worksheet
        lastRow = wsheet.Cells(wsheet.Rows.Count, 1).End(xlUp).Row
        
        ' Loop through each row in reverse order
        For i = lastRow To 1 Step -1
            ' Check if the entire row is blank
            If WorksheetFunction.CountA(wsheet.Rows(i)) = 0 Then
                ' If the row is blank, delete it
                wsheet.Rows(i).Delete
            End If
        Next i
    End Sub
    

    代码 2:从选定区域中删除空行

    Sub RemoveBlankRowsInRange()
    'UpdatebyExtendoffice
    Dim sRange As Range
    Dim row As Range
    ' Prompt the user to select a range
    On Error Resume Next
    Set sRange = Application.InputBox(prompt:="Select a range", Title:="Kutools for Excel", Type:=8)
    ' Check if a range is selected
    If Not sRange Is Nothing Then
    ' Loop through each row in reverse order
    For Each row In sRange.Rows
    ' Check if the entire row is blank
    If WorksheetFunction.CountA(row) = 0 Then
    ' If the row is blank, delete it
    row.Delete
    End If
    Next row
    Else
    MsgBox "No range selected. Please select a range and run the macro again.", vbExclamation
    End If
    End Sub
    

    A screenshot showing the VBA module window with code to remove blank rows in Excel

步骤 2:运行代码并删除空行

点击“运行”按钮或按“F5”键运行代码。

  • 如果您使用代码 1 删除活动工作表中的空行,运行代码后,工作表中的所有空行都将被删除。

  • 如果您使用代码 2 从选定区域中删除空行,运行代码后会弹出一个对话框,在对话框中选择要删除空行的区域,然后点击“确定”。

    A screenshot showing a dialog box for selecting a range to remove blank rows in Excel using VBA

结果:

代码 1:删除活动工作表中的空行

A screenshot showing the result of using VBA Code 1 to remove blank rows in the active sheet in Excel

代码 2:删除选定区域中的空行

A screenshot showing the result of using VBA Code 2 to remove blank rows from a selected range in Excel

删除包含空白单元格的行

 

本节分为两部分:一部分是使用“定位条件”功能删除包含空白单元格的行,另一部分是使用筛选功能删除在特定关键列中有空白的行。

>> 使用“定位条件”删除包含空白单元格的行

“定位条件”功能广泛推荐用于删除空行。当您需要删除至少包含一个空白单元格的行时,它是一个非常有用的工具。

步骤 1:选择范围内的空白单元格
  1. 选择要删除空行的范围,选择“开始”>“查找与选择”>“定位条件”。
    A screenshot showing the Go To Special feature in Excel for selecting blank cells
    或者,您可以直接按“F5”键启用“定位”对话框,然后点击“特殊”按钮切换到“定位条件”对话框。
  2. 在“定位条件”对话框中,选择“空白”选项并点击“确定”。
    A screenshot showing the Go To Special dialog in Excel with the Blanks option selected

现在,所选范围内的所有空白单元格都已被选中。

A screenshot showing blank cells selected in the range using Go To Special in Excel

步骤 2:删除包含空白单元格的行
  1. 右键单击任何选中的单元格,并从上下文菜单中选择“删除”(或者可以使用快捷键“Ctrl” + “-”)。
    A screenshot showing the context menu to delete rows containing blank cells in Excel
  2. 在“删除”对话框中,选择“整行”选项并点击“确定”。
    A screenshot showing the Delete dialog in Excel with the Entire Row option selected
结果:

A screenshot showing the result after removing rows containing blank cells in Excel using Go To Special

注意:如上所述,只要某行包含至少一个空白单元格,该行就会被删除。这可能导致一些重要数据的丢失。如果数据集很大,您可能需要花费大量时间查找丢失的数据并恢复。因此,在使用此方法之前,我建议先备份。

>> 使用筛选功能删除关键列中包含空白单元格的行

当您有一个大型数据集,并希望基于某个关键列包含空白单元格的条件删除行时,Excel 的筛选功能可以是一个强大的工具。

步骤 1:筛选关键列中的空白单元格
  1. 选择数据集,点击“数据”选项卡,进入“排序和筛选”组,点击“筛选”以对数据集应用筛选。
    A screenshot showing how to apply a filter to a dataset in Excel using the Data tab
  2. 点击您希望用来删除行的关键列的“筛选箭头”。在此示例中,“ID”列是关键列,只勾选扩展菜单中的“空白”。点击“确定”。
    A screenshot showing the filter dropdown menu with the Blanks option selected in Excel

现在,关键列中的所有空白单元格都已被筛选出来。

A screenshot showing rows with blank cells filtered in the key column in Excel

步骤 2:删除行

选择剩余的行(点击行号并向下拖动以选择所有空白行),然后右键单击,在上下文菜单中选择“删除行”(或者可以使用快捷键“Ctrl” + “-”)。并在弹出的对话框中点击“确定”。

A screenshot showing how to delete filtered blank rows in Excel

步骤 3:在排序和筛选组中选择筛选以清除已应用的筛选

A screenshot showing how to clear the applied filter in Excel

结果:

A screenshot showing the result after removing rows with blank cells in the key column using the Filter feature in Excel

注意:如果您想根据两个或多个关键列删除空行,请重复步骤 1,逐一对关键列中的空白进行筛选,然后删除包含空白单元格的行。

 

最佳办公效率工具

🤖 Kutools AI 助手:基于智能执行生成代码创建自定义公式分析数据并生成图表调用 Kutools 函数等功能,彻底改变数据分析方式…
热门功能查找、高亮或标记重复项 | 删除空行 | 合并不丢失数据的列或单元格 | 四舍五入 ...
高级 LOOKUP多条件 VLookup | 多值 VLookup | 多表查找 | 模糊查找 ....
高级下拉列表快速创建下拉列表 | 从属下拉列表 | 多选下拉列表 ....
列管理器添加指定数量的列 | 移动列 | 切换隐藏列的可见状态 | 比较区域和列 ...
精选功能网格聚焦 | 设计视图 | 增强编辑栏 | 工作簿与工作表管理器 | 资源库(自动文本) | 日期提取 | 合并数据 | 加密/解密单元格 | 按列表发送电子邮件 | 超级筛选 | 特殊筛选(筛选粗体/斜体/删除线...)...
排名前 15 的工具集12 种文本 工具添加文本删除特定字符等) | 50 多 种图表 类型甘特图等) | 40 多种实用 公式基于生日计算年龄等) | 19 种插入 工具插入二维码根据路径插入图片等) | 12 种转换 工具小写金额转大写汇率转换等) | 7 种合并与分割 工具高级合并行分割单元格等) | 还有更多...

使用 Kutools for Excel 提升您的 Excel 技能,体验前所未有的高效。 Kutools for Excel 提供超过 300 种高级功能来提高生产力并节省时间。 单击此处获取您最需要的功能...


Office Tab 将标签式界面引入 Office,让您的工作更加轻松

  • 在 Word、Excel、PowerPoint 中启用标签式编辑和阅读
  • 在同一窗口的新标签页中打开和创建多个文档,而不是在新窗口中进行操作。
  • 将您的生产力提升 50%,每天为您减少数百次鼠标点击!
{# article_7359_afterDisplayContent #}