首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Excel VBA:如果单元格不包含表格中的值,则删除行

Excel VBA:如果单元格不包含表格中的值,则删除行
EN

Stack Overflow用户
提问于 2017-02-03 10:46:16
回答 1查看 3.9K关注 0票数 0

我有一个大约50,000行的数据表。每一行都有一个位置标识符(列C)和其他数据(列D-H)。我需要删除位置标识符与另一个表中73个值中的一个不匹配的所有行。

我需要使用VBA (而不是公式/条件格式/表格),因为我需要对新的工作表重复该操作数百次。

我找到的最接近的单词是"How to delete all cells that do not contain specific values (in VBA/Excel)“。然而,答案中提供的代码不起作用。没有错误,只是什么都没发生。

感谢大家的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-03 18:04:42

这就是了:

代码语言:javascript
运行
复制
Sub Remove_Rows()
Dim i As Long

i = Range("C" & Cells.Rows.Count).End(xlUp).Row ' Find the bottom row number

Do Until i = 1 ' This loops to the top row before stopping (assuming you have a header row that you want to keep)
    If WorksheetFunction.CountIf(Sheets("Sheet2").Range("A:A"), Cells(i, 3)) = 0 Then Cells(i, 1).EntireRow.Delete 'This checks to see if the value in the C column is in sheet2 - If not, deletes row
    i = i - 1 'This step backwards though you data rows - the reason to use this as opposed to "forward stepping" is to avoid issues causes on the count when a row is deleted
Loop

End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42015909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档