跳至主要内容

如何删除不符合另一工作表条件的行?

Author: Sun Last Modified: 2025-05-07

在同一工作表中删除不符合条件的行可能并不困难,但你知道如何删除另一工作表中不符合条件的行吗?如下所示。现在我有两个技巧可以帮助你快速解决这个问题。

使用VBA删除另一工作表中不符合条件的行

使用Kutools for Excel删除另一工作表中不符合条件的行

a screenshot of criteria in Sheet2 a screenshot of the orginal data in Sheet1 before deleting a screenshot of an right arrow a screenshot of data after deleting
 条件(在工作表2中)     之前(工作表1)          之后(工作表1)

使用VBA删除另一工作表中不符合条件的行

有一段VBA代码可以帮助你删除另一工作表中不符合条件的行。

1. 按 Alt + F11 显示 Microsoft Visual Basic for Applications 窗口。

2. 在窗口中,点击 插入 > 模块 以显示一个新的模块窗口,然后将以下VBA代码复制到模块窗口中。

VBA:删除Excel中另一工作表上不符合条件的行。

Sub DeleteRow()
'Updateby20140618
Dim rng As Range
Dim Rng1 As Range, Rng2 As Range
Dim arr1 As Variant
Dim arr2 As Variant
Dim dic2 As Variant
Dim OutArr As Variant
xTitleId = "KutoolsforExcel"
Set Rng1 = Application.Selection
Set Rng1 = Application.InputBox("Range1 :", xTitleId, Rng1.Address, Type:=8)
Set Rng2 = Application.InputBox("Range2:", xTitleId, Type:=8)
Set Rng1 = Rng1.Columns(1)
Set Rng2 = Rng2.Columns(1)
Set dic2 = CreateObject("Scripting.Dictionary")
arr1 = Rng1.Value
arr2 = Rng2.Value
For i = 1 To UBound(arr2, 1)
    xKey = arr2(i, 1)
    dic2(xKey) = ""
Next
Rng1.ClearContents
OutArr = Rng1.Value
xIndex = 1
For i = 1 To UBound(arr1, 1)
    xKey = arr1(i, 1)
    If dic2.Exists(xKey) Then
        OutArr(xIndex, 1) = xKey
        xIndex = xIndex + 1
    End If
Next
Rng1.Value = OutArr
End Sub
3. 点击 运行,一个 KutoolsforExcel 对话框弹出,供你选择要删除不符合条件的行的区域。见截图:

a screenshot of selecting the data you want to remove

4. 点击 确定,另一个对话框弹出,用于选择另一工作表中的条件,见截图:

a screenshot of selecting the criteria data

5. 现在所有不符合另一工作表条件的行都被移除了。


使用Kutools for Excel删除另一工作表中不符合条件的行

如果VBA代码对你来说有点难,你可以试试Kutools for Excel的“比较区域”功能。

Kutools for Excel 包含300多种便捷的Excel工具。免费试用30天无限制。立即获取

1. 选择要删除不符合条件行的工作表。然后点击 Kutools > 区域 > 单元格对比。见截图:

2. 在“ 单元格对比”对话框中,点击 区域B 中的按钮,然后你可以选择所需的条件区域。见截图:

a screenshot of deleting rows not matching criteria using Kutools for Excel
doc-arrow
a screenshot of deleting rows not matching criteria using Kutools for Excel 2

 

3. 然后点击 确定,返回到“比较区域”对话框,勾选“ 不同值”和“选择整行”。见截图:

a screenshot of deleting rows not matching criteria using Kutools for Excel option

4. 点击 确定,并退出“ 比较区域”对话框。然后你会看到与条件不同的值行被选中了。

a screenshot of selected items that will be deleted

5. 右键单击上下文菜单中的 删除 来删除另一工作表中不符合条件的行。

a screenshot of deleting rows

点击这里了解更多关于Kutools for Excel的“比较区域”功能。


相关文章:


最佳办公效率工具

🤖 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%,每天为您减少数百次鼠标点击!