【问题标题】:using different worksheet as a crossreference to filter values in sheet 1使用不同的工作表作为交叉引用来过滤工作表 1 中的值
【发布时间】:2017-01-19 06:41:57
【问题描述】:

我在构思解决此问题的方法时遇到了麻烦。我想根据工作表 2 中有效值的交叉引用过滤掉工作表 1、F 列的无效值。

我没有在 excel 中使用过滤器的经验,但也许这是使用此功能的好机会。

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

表 1。

表 2。

除了使用我不熟悉的过滤功能之外,我还在考虑创建一个字典对象并使用工作表 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

Here is a sample of the data I am using.

【问题讨论】:

  • (1) 你没有定义lastrow 或者给它一个值。 (2) 由于您已将范围定义为从 ColB 到 ColQ,rw.Cells(3) 是“事件名称”(colD) 而不是“试验名称”(ColC)。区域的同样问题
  • "fixed" version of your file: dl.dropboxusercontent.com/u/15526711/full%20test%20TW.xlsm BTW 最好在共享文件时删除与特定问题无关的所有代码 - 这样我就不必在我之前查看多个模块决定在您的文件上启用宏。许多人甚至不会下载包含代码的文件。
  • @TimWilliams 很高兴您发布了该修复程序,因为我修复了您的建议并使其运行,但它正在删除 B 到 D 列。查看您的代码,它一定是我定义范围的方式或最后一个。无论如何,感谢您对宏文件的提醒。我还在 AOI 交叉引用表中添加了一些内容,但这看起来不错。谢谢。

标签: excel vba filter


【解决方案1】:

未经测试:

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

【讨论】:

  • 谢谢,蒂姆。我尝试使用此代码,将子中的工作表名称更改为("full test").Range("B7:T" & lastrow),将函数中的工作表更改为("AOI crossref")。它在 For Each rw 行上给了我一个运行时错误 1004。
  • 我忘了将 .Rows 添加到该行的末尾(请参阅编辑)。尝试使用该修复程序。如果这不能解决问题,那么如果您可以分享一些示例数据,我将进行测试/调试。
  • 好吧,我已经用我尝试过的代码编辑了我的问题,并提供了示例数据的链接。 .rows 没用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-28
  • 1970-01-01
  • 1970-01-01
  • 2013-12-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多