查找一个列单元格是否在另一个表单中等于另一个单元格

我想知道是否一组特定的单元格使用VBA匹配另一个表单中的另一组单元格。 在我的情况下,我需要找出lastName, firstName单元格是否匹配。 在我提出的解决scheme中,我正在循环访问第一个表,获取员工姓名。 然后循环访问第二个表,获取员工姓名。 然后看看如果两个匹配。 这种方法成本太高,花费时间太长。 有没有更好的方法来做到这一点?

我的第一个表格包含6行,我的第二个表格可以包含100 +行。 太浪费时间了。

我正在考虑只search整个专栏,看看姓是否先匹配,如果匹配,那就去看看名字是否匹配……但是可能会有一些人姓氏相同。 。

这是我到目前为止。

 For i = 2 To managerRows 'Looping through the Managers Table empFirst = managerSheet.Cells(i, 1) empLast = managerSheet.Cells(i, 2) empName = (empLast & ", " & empFirst) For j = 3 To assignRows 'Looping through the Assignments table empLastAssign = assignSheet.Cells(i, 4) empFirstAssign = assignSheet.Cells(i, 5) empNameAssign = (empLastAssign & ", " & empFirstAssign) 'MsgBox (empNameAssign) ... Conditional statement comparing names ... Next j Next i 

我知道我没有条件声明,我没有打扰,因为我知道这个方法不是最好的。

我不能添加另一列连接第二个工作表名称,因为它们是从数据库中读取的,并保存在单独的列和名和名中。 无论如何,有没有办法,我可以连接名称而不添加另一列到第二张表,并试图find他们的方式? 那有意义吗?

如果我没有弄错, Find只能看到一列。 它可以看在两个?

UPDATE

我能够得到姓氏的第一次出现,但不能得到其他的名字。 我已经添加了另一个字段来匹配。 所以有三个领域现在匹配。 Last NameFirst NameProject Name 。 到目前为止,我的代码只会find第一个出现并留在那里。 我想我的循环顺序是错误的。

这是我迄今为止。

  For i = 2 To managerRows 'Looping through the Managers Table empLast = managerSheet.Cells(i, 1) empFirst = managerSheet.Cells(i, 2) empName = (empLast & ", " & empFirst) projectName = managerSheet.Cells(i, 3) managerLast = managerSheet.Cells(i, 4) managerFirst = managerSheet.Cells(i, 5) managerName = (managerLast & ", " & managerFirst) Set findRow = assignSheet.Range(assignSheet.Cells(3, 4), assignSheet.Cells(assignRows, 4)) 'Set a range to look for Last Name Set c = findRow.Find(empLast, LookIn:=xlValues) 'Find matching Last Name if it exists If Not c Is Nothing Then 'Last Name found Do Until c Is Nothing 'Is this in the wrong place? If Cells(c.Row, 5) = empFirst Then 'If first name matches If Cells(c.Row, 10) = projectName Then 'If project name matches. We found them MsgBox ("Found: " & empLast & ", " & empFirst & ": Project: " & projectName & " : in: " & c.Row) End If End If Set c = findRow.FindNext(c) 'Is this is the wrong place? Loop End If Set c = Nothing 'Is this in the wrong place? Next i 

看看'Is this in the wrong place? 为我的新循环。

更新2:解决

我已经使用findfindNext成功筛选了三列。 在一些好的答案的帮助下。 我将发布完成的版本。 我必须添加额外的陈述到我的filter才能去find下一个灵。 希望其他人可以从中学习,因为没有明确的答案用三个过滤列find

 For i = 2 To managerRows 'Looping through the Managers Table empLast = managerSheet.Cells(i, 1) empFirst = managerSheet.Cells(i, 2) empName = (empLast & ", " & empFirst) projectName = managerSheet.Cells(i, 3) managerLast = managerSheet.Cells(i, 4) managerFirst = managerSheet.Cells(i, 5) managerName = (managerLast & ", " & managerFirst) 'Focus Below this Set findRow = assignSheet.Range(assignSheet.Cells(3, 4), assignSheet.Cells(assignRows, 4)) 'Set a range to look for Last Name Set c = findRow.Find(empLast, LookIn:=xlValues) 'Find matching Last Name if it exists If Not c Is Nothing Then 'Last Name found Do Until c Is Nothing If Cells(c.Row, 5) = empFirst Then 'If first name matches If Cells(c.Row, 10) = projectName Then 'If project name matches. We found them MsgBox ("Found: " & empLast & ", " & empFirst & ": Project: " & projectName & " : in: " & c.Row) Set c = Nothing Else Set c = findRow.FindNext(c) End If Else Set c = findRow.FindNext(c) End If Loop End If Next i 

而不是使用两个循环,你可以只使用第一个循环,并利用findfunction。 我相信你会更快。

 For i = 2 To managerRows 'Looping through the Managers Table empFirst = managerSheet.Cells(i, 1) empLast = managerSheet.Cells(i, 2) empName = (empLast & ", " & empFirst) managerLast = managerSheet.Cells(i, 3) managerFirst = managerSheet.Cells(i, 4) managerName = (managerLast & ", " & managerFirst) MsgBox (empName & ", " & managerName) Set myRng = assignSheet.Range(assignSheet.Cells(3, 4), assignSheet.Cells(assignRows, 4) Set c = myRng.Find(empName, lookin:=xlValues) if Not c is Nothing Then 'you found last name, no look to see if first is a match if assignSheet.cells(c.row, 5) = empFirst then 'if it is, do something 'do whatever you need to do here else firstAddress = c.Address Do Set c = myRng.FindNext(c) if Not c is Nothing Then 'you found last name, no look to see if first is a match if assignSheet.cells(c.row, 5) = empFirst then 'if it is, do something 'do whatever you need to do here end if end if Loop While Not c Is Nothing And c.Address <> firstAddress end if end if Next i 

有关find更多信息,请看这里 。

你只需要知道它是否存在…然后使用COUNTIFS如:

 =COUNTIFS(A:A,"Name",B:B,"Lastname"....) 

如果不是0,则有一个匹配。

对于VBA来说

 Application.Countifs(Range("A:A"),"Name",Range("B:B"),"Lastname"....) 

如果您还有任何问题,请询问;)

编辑

…我需要他们存在的行号…

你从来没有说过! *愤怒的脸* …仍然可以以一种或多或less的方式做:

 Sub test() Dim val As Variant, rowNum As Variant With Sheets("Sheet1") val = Evaluate(Intersect(.Columns(1), .UsedRange).Address & "&"" --- ""&" & Intersect(.Columns(2), .UsedRange).Address) rowNum = Application.Match("name" & " --- " & "firstname", val, 0) If IsNumeric(rowNum) Then Debug.Print "Found at Row: " & rowNum Else Debug.Print "Nothing was found" End With End Sub 

在查找重复内容时,我通常会使用字典或集合。 这样我只需要循环一次。

 Sub FindDuplicates() Dim empFirst As String, empLast As String, empName As String Dim assignSheet As Worksheet, managerSheet As Worksheet Dim i As Long, lastRow As Long Dim d Set assignSheet = Sheet2 Set managerSheet = Sheet1 Set d = CreateObject("Scripting.Dictionary") With managerSheet lastRow = .Range("A" & Rows.Count).End(xlUp).Row For i = 2 To lastRow 'Looping through the Managers Table empFirst = .Cells(i, 1) empLast = .Cells(i, 2) empName = (empLast & ", " & empFirst) If Not d.exists(empName) Then d.Add empName, i Next End With With assignSheet lastRow = .Range("A" & Rows.Count).End(xlUp).Row For i = 2 To lastRow 'Looping through the Managers Table empFirst = .Cells(i, 4) empLast = .Cells(i, 5) empName = (empLast & ", " & empFirst) If d.exists(empName) Then Debug.Print "Match Found", empName, "assignSheet Row:" & i, "managerSheet Row:" & d(empName) End If Next End With End Sub 

在这里输入图像说明