vba - Excel VBA检查值是否不在范围内

标签 vba excel

我有两个应该相同的范围(尽管它们的排序可能不同)。我试图在 rangeA 中找到不在 rangeB 中的任何值。

我能够找到显示值是否在某个范围内匹配的示例,但如果不匹配则很难找到任何东西。

到目前为止,我有:

Sub Compare2()

    Dim test1, cell As Range
    Dim FoundRange As Range

    Set test1 = Sheets("Names").Range("A1:A5")
    For Each cell In test1

        Set FoundRange = Sheets("Queue & Status").Range("A1:A200").Find(what:=test1, LookIn:=xlFormulas, lookat:=xlWhole)

        If FoundRange Is Nothing Then
            MsgBox (cell & " not found")    
        End If

    Next cell
End Sub

但是当它们匹配时,它会将所有值显示为不匹配。

最佳答案

尝试这个

Sub Compare2()

    Dim test1 As Range
    Dim lookIn As Range
    Dim c As Range
    Dim FoundRange As Range

    Set test1 = Sheets("Names").Range("A1:A5")
    Set lookIn = Sheets("Queue & Status").Range("A1:A200")
    For Each c In test1

        Set FoundRange = lookIn.Find(what:=c.Value, lookIn:=xlFormulas, lookat:=xlWhole)

        If FoundRange Is Nothing Then
            MsgBox (c.Value & " not found")
        End If

    Next c
End Sub

关于vba - Excel VBA检查值是否不在范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40923814/

相关文章:

vba - 如何从 Excel 调用 Word 宏

vba - 我能找到什么 sub 正在调用一个函数吗?

excel - 手动从 csv 文件中删除行

ruby - 如何使用 RubyXL 创建日期格式的单元格?

c# - Excel XML 的 MIME 类型 (ASP.NET 3.5)

Excel vba 删除 "?"(问号)和 "*"(星号)

windows - PowerPoint 2013 在导出 OLE VBA 命令后失去焦点

vba - 使用变量访问其他文件中的单元格

mysql - 如何实现非程序员对 SQL 数据库的频繁批量更新。数据库每次都应该从 Excel 导入吗?

c# - 在 Excel 中更改边框的左、右、下和上