【问题标题】:Excel VBA: delete rows if cells do not contain values from a tableExcel VBA:如果单元格不包含表格中的值,则删除行
【发布时间】:2017-06-20 07:59:04
【问题描述】:

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

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

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

感谢您的帮助!

【问题讨论】:

  • 复制的代码不经过修改就不太可能工作。您必须相应地进行更改。如果您已经尝试过,请向我们展示失败的代码和语句。可能有人可以窥视并建议您解决问题。

标签: excel vba


【解决方案1】:

给你:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多