提示:其他語言是Google翻譯的。 你可以訪問 English 版本。
登入
x
or
x
x
註冊
x

or

如何根據Excel中的單元格值刪除行?

要根據Excel中的單元格值快速刪除或刪除多行,可能需要先選擇包含特定單元格值的整行,然後再刪除它們。 似乎沒有一種快速的方法來刪除基於單元格值的行,但使用VBA代碼。 這裡有一些快速的技巧來幫助你。

使用查找和替換功能根據一個單元格值刪除行

使用VBA代碼基於一個單元格值刪除行

根據一個或兩個單元格值刪除行

根據多個單元格值刪除行

按特定單元格值快速選擇整個行/列,然後輕鬆刪除選定的行/列!

Kutools for ExcelSelect SpecifiC Cells 如果單元格值與Excel中的特定值相匹配,Excel實用程序為Excel用戶提供了一個輕鬆選擇,以選擇整行或整列。 工作更輕鬆更獨特! 全功能免費試用30天!
如果包含特定值,則選擇特殊單元格選擇整個行列

Office選項卡在Office中啟用選項卡式編輯和瀏覽,使您的工作更輕鬆......
Kutools for Excel解決了您的大多數問題,並使您的生產率提高了80%
  • 重用任何東西: 將最常用或最複雜的公式,圖表和其他任何內容添加到您的收藏夾中,並在將來快速重複使用它們。
  • 超過20文本功能: 從文本字符串中提取數字; 提取或刪除部分文字; 將數字和貨幣轉換為英文單詞。
  • 合併工具:將多個工作簿和工作表合二為一; 合併多個單元格/行/列,而不會丟失數據; 合併重複的行和總和。
  • 拆分工具:根據價值將數據分割成多個工作表; 一本工作簿可轉換為多個Excel,PDF或CSV文件; 一列到多列。
  • 粘貼跳過 隱藏/過濾行; 數和總和 按背景顏色; 將個性化電子郵件批量發送給多個收件人。
  • 超級過濾器: 創建高級過濾方案並應用於任何工作表; 分類 按週,日,頻率等; 過濾 通過大膽,公式,評論......
  • 超過300強大的功能; 與Office 2007-2019和365一起使用; 支持所有語言; 在您的企業或組織中輕鬆部署。


箭頭藍色右泡 使用“查找和替換”功能根據單元格值刪除行

在Excel中,您可以應用強大的查找和替換功能輕鬆刪除基於特定單元格值的行。 請做如下操作:

1。 根據特定的單元格值選擇要刪除行的範圍,然後按下,打開“查找和替換”對話框 Ctrl + F 鍵同時。

2。 在查找和替換對話框中,輸入特定的單元格值(在我們的例子中,我們輸入 ) 進入 Find what 框中,然後單擊 Find All 按鈕。 看到下面的第一個截圖:

3。 選擇查找和替換對話框底部的所有搜索結果,然後關閉此對話框。 (備註:您可以選擇一個搜索結果,然後 Ctrl + A 鍵來選擇所有找到的結果。 看到上面的第二個截圖。)
然後你可以看到所有包含特定值的單元格都被選中。

4。 繼續右鍵單擊所選單元格並選擇 Delete 從右鍵單擊菜單。 然後檢查 Entire row 選項中彈出刪除對話框,然後點擊 OK 按鈕。 現在您將看到所有包含該特定值的單元格被刪除。 查看下面的截圖:

然後整個行已經基於該特定值被刪除。


箭頭藍色右泡 使用VBA代碼基於單元格值刪除行

使用以下VBA代碼,可以快速刪除具有特定單元值的行,請按照以下步驟操作:

1。 按 Alt + F11 鍵同時打開 Microsoft Visual Basic for applications 窗口,

2。 點擊 Insert > Module,並將以下代碼輸入到模塊中:

VBA:根據單元格值刪除整行

Sub DeleteRows()
'Updateby20140314
Dim rng As Range
Dim InputRng As Range
Dim DeleteRng As Range
Dim DeleteStr As String
xTitleId = "KutoolsforExcel"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address, Type:=8)
DeleteStr = Application.InputBox("Delete Text", xTitleId, Type:=2)
For Each rng In InputRng
    If rng.Value = DeleteStr Then
        If DeleteRng Is Nothing Then
            Set DeleteRng = rng
        Else
            Set DeleteRng = Application.Union(DeleteRng, rng)
        End If
    End If
Next
DeleteRng.EntireRow.Delete
End Sub

3。 然後點擊 Run 按鈕來運行代碼。

4。 在彈出的對話框中,請根據特定值選擇要刪除行的範圍,然後單擊 OK 按鈕。

5。 在另一個對話框中,請輸入您將刪除行的特定值,然後單擊 OK 按鈕。 看截圖:

然後你會看到整個行已經根據指定的值被刪除了。


箭頭藍色右泡 使用Kutools for Excel根據一個或兩個單元格值刪除行

如果你已經安裝 Kutools for Excel,其 Select Specific Cells 功能可以幫助您快速刪除具有特定值的行。 請做如下操作:

1。 根據特定值選擇要刪除行的範圍,然後單擊 Kutools > Select > Select Specific Cells。 看截圖:

2。 在打開選擇指定單元格對話框中,請檢查 Entire row 選項,選擇 Contains 低至 Specific type 下拉列表中,在右側框中輸入指定的值,然後單擊 Ok 按鈕(見上面的截圖)。
應用此功能後,會彈出一個對話框,並根據指定的條件向您顯示找到了多少個單元格。 請點擊 OK 按鈕關閉它。

3。 現在選擇具有特定值的整行。 請右鍵單擊選定的行,然後單擊 Delete 從右鍵菜單。 看下面的截圖:

備註:這 Select Specific Cells 功能支持通過一個或兩個特定值刪除行。 要根據兩個指定值刪除行,請在中指定另一個值 Specific type 選擇特定單元格對話框部分,如下面的截圖所示:

Kutools for Excel -包括多個用於Excel的300便捷工具。 全功能免費試用30天,無需信用卡! 立即行動吧


箭頭藍色右泡使用Kutools for Excel刪除基於多個單元格值的行

在某些情況下,您可能需要根據Excel中另一列/列表中的多個單元格值刪除行。 我將在這裡介紹 Kutools for Excel's Select Same & Different Cells 功能可以輕鬆快速解決問題。

1。 點擊 Kutools > Select > Select Same & Different Cells 打開“選擇相同和不同單元”對話框。

2。 在開幕式上 Select Same & Different Cells 對話框,請按如下操作(見截圖):

(1)在 Find values in 框中,請選擇您將找到某些值的列;
(2)在 According to 框中,請選擇具有多個值的列/列表,您將基於此刪除行;
(3)在 Based on 部分,請檢查 Each row 選項;
(4)在 Find 部分,請檢查 Same Values 選項;
(5)檢查 Select entire rows 選項在打開對話框的底部。
備註:如果兩個指定的列包含相同的標題,請檢查 My data has headers 選項。

3。 點擊 Ok 按鈕以應用此實用程序。 然後出現一個對話框並顯示已選擇了多少行。 只需點擊 OK 按鈕關閉它。

然後選擇包含指定列表中的值的所有行。

4。 點擊 Home > Delete > Delete Sheet Rows 刪除所有選定的行。


箭頭藍色右泡演示:根據Excel中的一個或多個單元格值刪除行

Kutools for Excel 包含多個用於Excel的300便捷工具,可以在30天內不受限制地免費試用。 立即下載並免費試用!

Kutools for Excel解決了您的大多數問題,並使您的生產率提高了80%

  • 重用: 快速插入 複雜的公式,圖表 以及你以前用過的任何東西; 加密單元格 密碼; 創建郵件列表 並發送電子郵件...
  • 超級方程式酒吧 (輕鬆編輯多行文字和公式); 閱讀佈局 (輕鬆讀取和編輯大量單元格); 粘貼到過濾範圍...
  • 合併單元格/行/列 不丟失數據; 分裂細胞含量; 組合重複的行/列...防止重複的細胞; 比較範圍...
  • 選擇複製或唯一 行; 選擇空行 (所有細胞都是空的); 超級查找和模糊查找 在許多工作簿中; 隨機選擇......
  • 精確複製 多個單元格而不更改公式參考; 自動創建參考 多張表; 插入項目符號,複選框等等......
  • 提取文本,添加文本,按位置刪除, 刪除空間; 創建和打印分頁小計; 在單元格內容和註釋之間轉換...
  • 超級過濾器 (將過濾方案保存並應用到其他工作表); 高級排序 按月/週/日,頻率等; 特殊過濾器 用粗體,斜體......
  • 結合工作簿和工作表; 根據鍵列合併表; 將數據拆分為多個表格; 批量轉換xls,xlsx和PDF...
  • 超過300強大的功能。 支持Office / Excel 2007-2019和365。 支持所有語言。 在您的企業或組織中輕鬆部署。 全功能30天免費試用。
kte tab 201905

Office選項卡為Office提供選項卡式界面,使您的工作更輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,Publisher,Access,Visio和Project。
  • 在同一窗口的新選項卡中打開並創建多個文檔,而不是在新窗口中。
  • 通過50%提高您的工作效率,每天為您減少數百次鼠標點擊!
官方底部
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    kaushal · 2 years ago
    very well. Thank you!
  • To post as a guest, your comment is unpublished.
    Bobby · 2 years ago
    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
  • To post as a guest, your comment is unpublished.
    Amar · 2 years ago
    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..
  • To post as a guest, your comment is unpublished.
    Marion · 3 years ago
    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

    [i][b]My real case is:[/b][/i] 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
  • To post as a guest, your comment is unpublished.
    Aju Thoas · 3 years ago
    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.
  • To post as a guest, your comment is unpublished.
    PC · 3 years ago
    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
  • To post as a guest, your comment is unpublished.
    NEU · 3 years ago
    thank you so much. That save me a lot of time
  • To post as a guest, your comment is unpublished.
    NC Arch · 4 years ago
    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.
  • To post as a guest, your comment is unpublished.
    Clay · 4 years ago
    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
  • To post as a guest, your comment is unpublished.
    Milon · 4 years ago
    Great it work thank you so much
  • To post as a guest, your comment is unpublished.
    Moe · 4 years ago
    Hello, many thanks for the code. But say I want to delete one row containing "Apple". And not all of the rows containing it. Let's just say the last one, or a random one, doesn't really matter, just one. Many thanks in advance! :-)
  • To post as a guest, your comment is unpublished.
    Meredith · 4 years ago
    Just what I needed, thanks! :D
  • To post as a guest, your comment is unpublished.
    JonS · 4 years ago
    Is there anyway to have the value recognize a value that has expired, such as. All rows that show an expiration date within a specific column will be automatically removed and then the rows that were deleted will be filled by moving the rows beneath. Filling gaps.
  • To post as a guest, your comment is unpublished.
    angel baby · 4 years ago
    What a great. thank so all so much. :)
  • To post as a guest, your comment is unpublished.
    DavidGough · 4 years ago
    is it possible to compare two columns and then delete? is in range A3:D3000 if cell A=x and cell B=y then delete row?
  • To post as a guest, your comment is unpublished.
    DavidGough · 4 years ago
    How can I specify the range A3:D3000 (ie. I don´t want the user to select the range)? How can I make the Delete String a cell reference, ex. G1?
  • To post as a guest, your comment is unpublished.
    Joan K · 4 years ago
    Hello and thank you very much for the VBA code. I have one question.

    Is it possible to make the code the way that it is possible to choose several cell values to delete at the same time? For example choose Apple and Emily at the same time and delete them?
    I appreciate you work and looking forward to your response.

    Joan K
  • To post as a guest, your comment is unpublished.
    Victor · 5 years ago
    i have a list of stores in one document. I have a report that returns thousand of lines. the store number is always in Column A. Is there a code/macro that will search the report and delete all columns with store numbers from the store list. thinking a vlookup and "Do While" are needed.
  • To post as a guest, your comment is unpublished.
    alex2 · 5 years ago
    I'd like to delete all rows where column1 = "Apple" AND column3 = "green". please?
    • To post as a guest, your comment is unpublished.
      Finidi · 5 years ago
      Im not sure how to do this using macro's or so, but one option that would do its job just fine is to add an extra colum that tests for the one colum to be "apple" and the other to be "green" and then let it give a simple "yes" or "no" value. than use either of the above options to search for the value "yes" and remove rows based on that instead of 2 seperate values.
  • To post as a guest, your comment is unpublished.
    gabz · 5 years ago
    thank.. it helps me a lot...
  • To post as a guest, your comment is unpublished.
    smj2013 · 5 years ago
    Thank you for the VB Script. I can not wait to try this. Question:Is there anyway to mark the columns for deletion prior to deleting them and/or copy to a separate tab in the workbook? In addition is there a way to run this script run on multiple workbooks/files at one time?
  • To post as a guest, your comment is unpublished.
    Leo · 5 years ago
    Hi,
    Any way to write it to delete a row, with an OR exception? I.e- delete rows that are duplicates, but ignore a certain value. I want to delete duplicates, except where the column in question contains a blank b/c that column doesn't have valid data yet. I ran as you have, but I ended deleting rows with a blank in the column in question, so I can't use it as it is.
    Thanks
  • To post as a guest, your comment is unpublished.
    Tee · 5 years ago
    Hi,
    What is the easiest way to delete rows that do NOT contain "Apple" please?

    Thanks
  • To post as a guest, your comment is unpublished.
    Aleardo · 5 years ago
    Thanks, this is great!
    Quick comment, I tried the find and replace function in excel 2010.
    In order to select all the found results I had to use Ctrl +A instead of Alt + A.
  • To post as a guest, your comment is unpublished.
    Mira · 5 years ago
    Definitely awsome! Appreciate for sharing!!!
  • To post as a guest, your comment is unpublished.
    Joe Nelson · 5 years ago
    I just wanted to say thank you. This worked like a charm.
  • To post as a guest, your comment is unpublished.
    Adam · 5 years ago
    Lets say I put the below code in to delete all of my rows that contain apple but then I want it to continue once that is done and delete all the rows that contain banana? I tried to just duplicate the code but it seems to stop after apple.

    thanks.

    Sub Delete_Rows()
    Dim rng As Range, cell As Range, del As Range
    Set rng = Intersect(Range("A1:C20"), ActiveSheet.UsedRange)
    For Each cell In rng
    If (cell.Value) = "Apple" _
    Then
    If del Is Nothing Then
    Set del = cell
    Else: Set del = Union(del, cell)
    End If
    End If
    Next cell
    On Error Resume Next
    del.EntireRow.Delete
    End Sub
  • To post as a guest, your comment is unpublished.
    H man · 5 years ago
    I get:

    run-time error '13':
    Type mismatch

    Debugging highlights the If (cell.Value) = "FALSE" _
    Then
    section.

    I'm totally lost in MVB, any help would be appreciated.
  • To post as a guest, your comment is unpublished.
    Jim Mc · 5 years ago
    Heartfelt thanks - it's always amusing when the Marketing guy tries to write code, and this info was super helpful. I was unable to get the code to span multiple columns ("AA2:AA3000" works, "AB2:AB3000" works, but "AA2:AB3000" doesn't work. There's no Earth-shattering Kaboom - it just does nothing. I've solved it (amateurishly, I suppose) by running several macros in sequence, but there's probably a more elegant way. Thanks again for your help, Jim (Melville, NY)
  • To post as a guest, your comment is unpublished.
    Claire · 5 years ago
    Hey - thanks so much for the script. I was wondering if you can use wildcards so you can select anything within a cell that matches rather than a specific item? I've tried using ** wildcards but it doesn't seem to do anything. Here is what I have:

    Sub Delete_Rows()
    Dim rng As Range, cell As Range, del As Range
    Set rng = Intersect(Range("B6:B20"), ActiveSheet.UsedRange)
    For Each cell In rng
    If cell.Value Like "*WORDTODELETE*" _
    Then
    If del Is Nothing Then
    Set del = cell
    Else: Set del = Union(del, cell)
    End If
    End If
    Next cell
    On Error Resume Next
    del.EntireRow.Delete
    End Sub
  • To post as a guest, your comment is unpublished.
    Art · 6 years ago
    Bobby, try this, it should get what you want:
    If (cell.Value) = "Apple" OR (cell.Value) = "Monday"
    • To post as a guest, your comment is unpublished.
      April · 5 years ago
      Is there anyway to have the value recognize a value whether is > or = to a certain value?
  • To post as a guest, your comment is unpublished.
    Bobby · 6 years ago
    Any way to modify the If (cell.Value) = "Apple" to include multiple values?
  • To post as a guest, your comment is unpublished.
    Alan · 6 years ago
    Fantastic! That worked a treat, I altered it slightly to suit what I needed but pure genious. Oh VBA Version.