首页 > 解决方案 > 在多个工作表中查找重复项

问题描述

我想查找 2 张纸中是否有重复项。

示例 1(适用于此工作表):

Function FindDuplicate(factnr) As Boolean

    With Worksheets("Sheet 1").Range("D6:D206")

        Set C = .Find(factnr, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
        If Not C Is Nothing Then
            FindDuplicate = True
            Exit Function
        End If
    End With

    FindDuplicate = False
End Function

示例2(这是我要完成的[模拟代码],检查两张表的功能)

Function FindDuplicate(factnr) As Boolean

   With Worksheets("Sheet 1").Range("D6:D206") & ("Sheet 2").Range("D6:D206")

     Set C = .Find(factnr, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
     If Not C Is Nothing Then
         FindDuplicate = True
         Exit Function
     End If
   End With

   FindDuplicate = False
End Function

标签: excelvba

解决方案


你不能像那样加入范围,你需要单独搜索每个

Function FindDuplicate(factnr) As Boolean

 With Worksheets("Sheet 1").Range("D6:D206") 

     Set C = .Find(factnr, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
     If Not C Is Nothing Then
        FindDuplicate = True
        Exit Function
      End If
End With
     With Worksheets("Sheet 2").Range("D6:D206")

     Set C = .Find(factnr, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
     If Not C Is Nothing Then
        FindDuplicate = True
        Exit Function
      End If
End With


FindDuplicate = False
End Function

推荐阅读