跳到主要內容

根據 Excel 中的儲存格值刪除行 – 簡單教學課程

無論您處理的是小型電子表格還是大型資料集,請了解如何根據儲存格值刪除行都可以節省您的時間和精力。此過程可以使用多種 Excel 功能來執行,包括篩選功能、尋找和取代工具、排序功能,甚至可以使用進階技術。在本指南結束時,您將具備從 Excel 工作表中有效刪除不需要的行的知識,確保您的資料乾淨且符合您的分析需求。

例如,如果我想刪除 name 欄位包含 Jener 的所有行,我該如何進行?

根據儲存格值刪除整行

使用排序功能根據儲存格值刪除選擇中的行


根據 Excel 中的儲存格值刪除整行

Excel 提供了多種根據特定儲存格值刪除資料列的方法。本節涵蓋的方法範圍從簡單的 Excel 功能(如過濾、尋找和替換和 Kutools)到涉及 VBA 的更高級技術。無論您是初學者還是高級用戶,您都會找到適合您的技能水平和需求的方法。

📝 備註:以下刪除行的方法將刪除該行中的所有數據,包括該行其他儲存格中的內容。為了防止資料遺失,請考慮在刪除之前將資料集複製到另一個工作表上。

使用篩選器功能根據儲存格值刪除整行

通常,您可以套用篩選功能來顯示符合特定條件的行,然後立即刪除該行。

步驟1:套用過濾功能過濾掉不需要的行

  1. 選擇要刪除行的資料範圍。然後,點擊 數據 > 篩選,請參見屏幕截圖:
  2. 現在,下拉箭頭將出現在每個欄位的標題中。按一下包含要根據其刪除行的值的列中的下拉箭頭。例如,如果您要刪除名稱為 Jener 的行,則可以按一下「名稱」列中的箭頭。
  3. 在下拉式選單中,取消選中 選擇全部 清除所有選擇,然後僅選取 Jener 旁邊的方塊,或在搜尋框中手動輸入 Jener 以快速找到它。
  4. 然後,單擊 OK 按鈕來套用過濾器。僅顯示包含 Jener 的行。看截圖:

步驟 2:刪除篩選的行

選擇可見行,然後右鍵單擊所選行之一併選擇 刪除行 從上下文選單中。這將刪除所有選取的行。

步驟 3:清除過濾器

然後,點擊 數據 > 篩選 再次。這將刪除過濾器,您將看到除已刪除記錄之外的所有記錄。

💡 保養竅門:
  • 與我應用過濾方法來消除包含文字 Jener 的行類似,您也可以利用它根據數字或日期條件刪除行,如以下螢幕截圖所示:
    號碼過濾條件:


    日期過濾條件:
  • 您也可以使用“過濾器”功能來過濾和刪除包含特定背景顏色的所有行。看截圖:
  • 您可以使用過濾功能根據多個條件進行過濾,過濾掉不需要的內容,然後將其刪除。例如,假設您要刪除銷售員為 Jener 且總銷售額大於 7000 美元的所有行。您應該先按名稱 Jener 過濾行,然後過濾銷售額大於 7000 的行。
 

使用 Kutools AI Aide 根據儲存格值刪除整行

Excel的Kutools 是一個綜合性加載項,包含 300 多個進階功能,旨在簡化 Excel 中的各種複雜任務。它的特點之一是, Kutools 人工智慧助手,有助於自動執行任務,例如根據特定條件過濾和刪除行。

📝 備註: 要使用這個 Kutools 人工智慧助手 of Excel的Kutools下載並安裝 Kutools for Excel 第一。

安裝後 Excel的Kutools請點擊 Kutools人工智慧 > 人工智慧助手 打開 Kutools 人工智慧助手 窗格:

  1. 選擇資料範圍,然後在聊天框中輸入您的要求,然後按一下 送出 按鈕或按下 Enter 發送問題的按鍵;
    “如果“名稱”列在選擇中包含名稱 Jener,則刪除行”
  2. 分析完後點擊 執行 按鈕運行。 Kutools AI Aide 將使用 AI 處理您的請求並直接在 Excel 中刪除指定的行。
💡 保養竅門:
  • 若要根據多個條件刪除行,只需相應地調整要求即可。例如,使用類似“刪除所選內容中「名稱」欄位包含 Jener 或 Kevin 的行“或”刪除所選內容中「名稱」欄位包含 Jener 且總銷售額超過 7000 的行".
  • 這個方法 不支援撤銷 功能。但是,如果您想恢復原始數據,您可以單擊 不滿意 還原更改。
 

使用尋找和取代功能根據儲存格值刪除整行

使用 Excel 的尋找和取代功能根據特定儲存格值刪除整行是一種簡單的方法,可以非常有效地清理資料。請依照以下步驟操作:

步驟 1:應用查找和取代功能來選擇特定值

  1. 選擇要根據特定儲存格值刪除行的列數據,然後開啟 查找和替換 按下對話框 Ctrl + F 鍵同時。
  2. 查找和替換 在對話方塊中,請鍵入特定的儲存格值(在我們的例子中,我們輸入 耶納) 進入 查找內容 框中,然後單擊 找到所有 按鈕。 看截圖:
  3. 選擇底部的所有搜尋結果 查找和替換 對話框,然後關閉該對話框。 (您可以選擇其中一項搜尋結果,然後 按Ctrl + A 鍵選擇所有找到的結果。)並且包含特定值的所有儲存格都被選取。然後,關閉該對話框。看截圖:

步驟 2:根據所選值刪除行

  1. 在儲存格仍處於選取狀態的情況下,以滑鼠右鍵按一下選取的儲存格之一,然後選取 刪除 從上下文菜單。
  2. 選擇 整行 在「刪除」對話方塊中,然後按一下 OK 刪除包含指定值的所有行。
 

使用 VBA 程式碼根據儲存格值刪除整行

在 Excel 中使用 VBA(Visual Basic for Applications)根據儲存格值刪除資料列可以實現任務自動化,從而提高效率,尤其是對於大型資料集。

第 1 步:打開 VBA 模塊編輯器並複制代碼

  1. 媒體推薦 Alt + F11鍵 鍵打開 Microsoft Visual Basic for Applications 窗口。
  2. 在打開的視窗中,按一下 插入 > 模塊 創建一個新的空白模塊。
  3. 然後,將以下程式碼複製並貼上到空白模組中。
    Sub DeleteRowsBasedOnCellValue()
    'Updateby Extendoffice
        Dim ws As Worksheet
        Set ws = ActiveSheet
        Dim columnRange As Range
        On Error Resume Next
        Set columnRange = Application.InputBox("Select the column range to check:", "Kutools for Excel", Type:=8)
        On Error GoTo 0
        If columnRange Is Nothing Then Exit Sub
        Dim lastRow As Long
        lastRow = ws.Cells(ws.Rows.Count, columnRange.Column).End(xlUp).Row
        Dim criteria As String
        criteria = Application.InputBox("Enter the value to delete rows for:", "Kutools for Excel", Type:=2)
        If criteria = "" Then Exit Sub
        Dim i As Long
        For i = lastRow To 1 Step -1
            If ws.Cells(i, columnRange.Column).Value = criteria Then
                ws.Rows(i).Delete
            End If
        Next i
    End Sub
    

第2步:執行程式碼

  1. 然後按 F5 鍵來運行程式碼。在彈出的對話方塊中,選擇要根據特定值刪除行的列,然後按一下 OK 按鈕。
  2. 在下列對話方塊中,請鍵入要刪除行的特定值,然後按一下 OK 按鈕。 看截圖:

結果:

然後您將看到已經基於指定的值刪除了整個行。

📝 備註:這個VBA程式碼方法 不支援撤銷,因此在使用之前請確保操作按預期執行。此外,建議維護資料備份,以防止意外變更或遺失。

使用排序功能根據儲存格值刪除選擇中的行

上述所有方法都會刪除整行,這可能是個限制。例如,使用這些方法將消除資料集右側的所有資料。假設您希望僅刪除資料集中的某些記錄,同時保留其餘資料。在這種情況下,需要採用替代方法。

步驟 1:為資料建立輔助列

建立一個新列,這將幫助我們追蹤訂單。您需要在資料旁邊插入一列,然後用序號填滿它,例如 1、2、3…請參見螢幕截圖:

步驟2:根據特定列對資料進行排序

  1. 選擇要刪除行所依據的資料列。然後,點擊 數據 > 將A到Z排序 or Z到A,彈出排序警告對話框,選擇 擴大選擇,然後單擊 分類 按鈕。 看截圖:
  2. 現在,具有相同值的行將被分組在一起,從而可以輕鬆識別和選擇它們。看截圖:

步驟 3:刪除已排序的行

  1. 選擇要刪除的記錄組,然後右鍵單擊,然後選擇 刪除 從上下文菜單中。 在裡面 刪除 對話框中選擇 向上移動單元格 選項。然後,點擊 OK 按鈕看截圖:
  2. 僅刪除名稱為 Jener 的記錄,而不刪除整行。因此,資料集右側或左側的任何資料將不受影響。

步驟 4:應用排序功能恢復資料的原始順序

按一下幫助列的標題,然後前往 數據 在功能區上並選擇 將A到Z排序 根據輔助列中的順序組織資料。這將根據需要將所選範圍內的資料還原為其原始順序。

第 5 步:刪除輔助列

最後,根據需要刪除輔助列以清理工作表。


在本文中,我們探索了幾種根據儲存格值刪除 Excel 中的行的方法。您可以選擇您喜歡的。如果您有興趣探索更多 Excel 提示和技巧,我們的網站提供了數千個教程,請 點擊此處訪問它們。 感謝您的閱讀,我們期待在未來為您提供更多有用的信息!


相關文章:

  • 輕鬆刪除 Excel 中的空白儲存格 - 完整教學課程
  • 刪除 Excel 中的空白儲存格是一項常見任務,可以幫助簡化數據,使其更易於分析、理解和呈現。空白儲存格會擾亂您的資料分析,導致公式錯誤,並使您的資料集看起來不完整或不專業。在本教學中,我們將探索幾種有效刪除或管理 Excel 中的空白儲存格的方法,例如「前往特殊功能」、公式、篩選功能。每種方法都滿足不同的需求和場景,因此您可以選擇最適合您的要求的方法。
  • 根據背景顏色刪除行
  • 如何根據背景色刪除整個行? 在此示例中,我需要刪除所有單元格填充有藍色背景色的所有行,如下面的屏幕截圖所示。 通過本文,您將獲得一些代碼以在Excel中完成此任務。
  • 刪除每隔一行
  • 如果要快速刪除Excel中的每隔一行或每一列,重點是如何首先快速選擇每隔一行或每一列,然後對它們應用刪除操作。 本文將向您展示一些棘手的事情,有關如何首先選擇每隔一行或每一列然後快速刪除它們。
Comments (39)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
In given range, if any cell contains defined text the entire row gets deleted.

Pls share code
This comment was minimized by the moderator on the site
Hi Yogesh,
On this webpage, the second method is to delete rows by cell values with VBA code. You can go to there and copy the code directly!
https://www.extendoffice.com/documents/excel/815-excel-remove-rows-based-on-cell-value.html#vba
This comment was minimized by the moderator on the site
There are two issues with the code posted as of this date. 1) the variable xTitleId is not declared, which causes a compilation error if Option Explicit is in use. 2) With Excel 2016, it appears that a range is limited to 129 areas. Each discontiguous matching cell found in the loop creates another area in the DeleteRng variable. Unfortunately, the Union method does not raise an error if that number of areas is exceeded; it just ignores the remaining cells. When the loop is finished, DeleteRng will have no more than 129 areas.
This comment was minimized by the moderator on the site
Hi TomTheToolman,
Thank you so much for your feedback. I've already updated the new code in the article. If you have any other questions, please don't hesitate to let me know.
Amanda
This comment was minimized by the moderator on the site
very well. Thank you!
This comment was minimized by the moderator on the site
Can someone help me...If 3rd column has value 0, then delete all values of corresponding column 1st. In this case answer should be the last line only.... Check MenuName ID 3149 VNLA MILFLLE 2 3149 TURKEY PNN 0 3149 R. BEEF PNN 0 3149 MIX MOCHA 38 3150 M.G.R 1/2 0 3150 THE PEPPE L 0 3150 MIX SLD 0 3150 EGGPLANT 0 3150 STILL WATER 7 3151 MIX MOCHA 38
This comment was minimized by the moderator on the site
Thanks for sharing. I am actually looking for a code that doesn't ask user for range but instead selects a specific column say column "A" and runs till the last row of that column. Can you please help..
This comment was minimized by the moderator on the site
Hi everybody, I am wondering what can we do to delete the following (According to the example shown in this page): Soe appears at several date (sept, October... etc). What I would like is to delete the line where Soe is but to keep the line with last date she appeared. In addition, some lines could be in double but I still want to keep it. So for example, you have the lines: - July 3 /Soe - Sep 4 / Soe - Sep 4 / Soe - Oct 19/ Soe - Nov 13 / Soe - Nov 13 / Soe and what I want to keep is: - Nov 13 / Soe - Nov 13 / Soe My real case is: I have different EAN code and version 1, 2, 3 or 4 and I want to keep the line where the version is the higher. e.g.: I have: - EAN 1 / Version 1 - EAN 1 / Version 1 - EAN 1 / Version 2 - EAN 1 / Version 2 - EAN 2 / Version 2 - EAN 2 / Version 3 - EAN 2 / Version 3 and I want to keep: - EAN 1 / Version 2 - EAN 1 / Version 2 - EAN 2 / Version 3 - EAN 2 / Version 3 I am searching since hours and I am completely blocked on this issue. Many thanks in advance for your brain and help. Best, Marion
This comment was minimized by the moderator on the site
Hi, Thank you this was really helpful. However, there's an error that pops up when I run the codes it says "Object variable or with block variable not set" and it points to " the line DeleteRNG.EntireRow.Delete". Could you please help me with debugging this. Thanks.
This comment was minimized by the moderator on the site
I am trying to use this macro in order to delete unused formulas, because excel views blank formula cells as a zero value and will print extra pages. I was hoping when I deleted the unused formulas, when I printed it would only print the pages that had information. This is not the case and I really need help to find a solution. I have tried using this formula and it is not working and prints three extra pages that I do not need even with the extra formulas being deleted. Sub selectonly() ' ' selectonly Macro ' Range("A1").Select Range(Selection, Selection.End(xlToRight)).Select Range(Selection, Selection.End(xlDown)).Select ExecuteExcel4Macro "PRINT(1,,,1,,,,,,,,2,,,TRUE,,FALSE)" End Sub
This comment was minimized by the moderator on the site
thank you so much. That save me a lot of time
This comment was minimized by the moderator on the site
Trying to find a delete Function that will delete entire designated rows automatically, based on certain values or certain text contained in other cell(s), using only automated formulas.
This comment was minimized by the moderator on the site
How can i delete selected cell that i want based on value that i entered for example : A1 B1 C1 D1 A2 B2 C2 D2 A3 B3 C3 D3 A4 B4 C4 D4 When i entered in some cell for example "2" then 2 row will be deleted from A3:D4. If i entered "1" then 1 row will be deleted from A4:D4. if i entered "3" then 3 row will be deleted from A2:D4
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations