Skip to main content

6 種簡單方法刪除 Excel 中的空行(逐步教學)

Author: Sun Last Modified: 2025-05-12

當您處理包含空行的大型數據集時,這些空行可能會使您的工作表混亂並妨礙數據分析。雖然您可以手動刪除少量的空行,但當面對數百個空行時,這種方法將變得耗時且效率低下。在本教程中,我們介紹了六種不同的方法,可以高效地批量刪除空行。這些技術涵蓋了您在 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 個插入工具插入QR碼根據路徑插入圖片、...)   |   12 個轉換工具金額轉大寫匯率轉換、...)   |   7 個合併與分割工具高級合併行分割儲存格、...)   |   ... 還有更多

使用 Kutools for Excel 提升您的 Excel 技能,體驗前所未有的高效。 Kutools for Excel 提供超過 300 種高級功能來提高生產力並節省時間。  點擊這裡獲取您最需要的功能...


Office Tab 將標籤式界面帶到 Office,讓您的工作更加輕鬆

  • 在 Word、Excel、PowerPoint 中啟用標籤式編輯和閱讀
  • 在同一窗口的新標籤中打開和創建多個文檔,而不是在新窗口中。
  • 提升 50% 的生產力,每天為您減少數百次鼠標點擊!