Excel VBA:如果单元格不包含表

时间:2017-02-03 02:46:16

标签: excel-vba vba excel

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

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

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

感谢那里的任何帮助!

1 个答案:

答案 0 :(得分:0)

你走了:

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