使用不同的工作表作为交叉引用来过滤表1中的值

我很难解决这个问题。 我想基于表单2中的有效值的交叉引用过滤掉表单1列F的无效值。

我没有在Excel中使用filter的经验,但也许这是使用此function的好机会。

困难在于表1有多个试验(c栏),每个试验都有多个有效值,如这些屏幕截图所示。

表1 表1

工作表2 表2

除了使用我不熟悉的过滤function外,我正在考虑创build一个字典对象,并使用表1中的列C或表2中的列A作为关键字,但是我不确定如何使程序检查多个值,而不使用遍历表2的每个单元格以及表1中的列F的大的长嵌套循环。

我想要实现的基本任务是: For trial #, sheet 1, if column F =/= (a value in row #, sheet 2), then delete row.

更新:我已经尝试了这个代码,它在“for each”行上返回一个运行时1004应用程序或对象定义的错误:

 Sub RemoveNonMatch() Dim rngDel As Range, rw As Range For Each rw In ThisWorkbook.Sheets("full test").Range("B7:Q" & lastrow).Rows If Not IsMatch(rw.Cells(3).Value, rw.Cells(6).Value) Then If Not rngDel Is Nothing Then Set rngDel = Application.Union(rngDel, rw) Else Set rngDel = rw End If End If Next rw 'remove any non-matches If Not rngDel Is Nothing Then rngDel.Delete End Sub Function IsMatch(TrialNum, AreaNum) As Boolean Dim t, a, rv rv = False With ThisWorkbook.Sheets("AOI crossref") 'try to find TrialNum in the first column t = Application.Match(TrialNum, .Columns(1), 0) If Not IsError(t) Then 'try to find AreaNum in the m'th row a = Application.Match(AreaNum, .Rows(t), 0) If Not IsError(a) Then rv = True 'match! End If End With IsMatch = rv End Function 

这是我正在使用的数据的一个示例。

未经testing:

 Sub RemoveNonMatch() Dim rngDel As Range, rw As Range For Each rw In ThisWorkbook.Sheets("Sheet2").Range("A2I100").Rows 'or whatever... If Not IsMatch(rw.Cells(3).Value, rw.Cells(6).Value) Then If Not rngDel Is Nothing Then Set rngDel = Application.Union(rngDel, rw) Else Set rngDel = rw End If End If Next rw 'remove any non-matches If Not rngDel Is Nothing Then rngDel.Delete End Sub Function IsMatch(TrialNum, AreaNum) As Boolean Dim t, a, rv rv = False With ThisWorkbook.Sheets("Sheet2") 'try to find TrialNum in the first column t = Application.Match(TrialNum, .Columns(1), 0) If Not IsError(t) Then 'try to find AreaNum in the m'th row a = Application.Match(AreaNum, .Rows(t), 0) If Not IsError(a) Then rv = True 'match! End If End With IsMatch = rv End Function