VBA excel,在列中find相应的值

我设法遍历两列来查找一列中的值是否出现在另一列中。

现在下一步是确定两列中的值是否在值右侧的单元格中包含相同的值。

期望的结果和当前结果的图片应该解释我想达到的目的。

PS不要被代码弄糊涂,因为我的工作簿中的列在两个不同的表单上。

在这里输入图像说明

一旦我得到一场比赛的确认,我已经尝试了循环两次通过列,但在这一点上,我只是失去了….对不起

Sub loopDb() Set dbsheet1 = ThisWorkbook.Sheets("Sheet1") Set dbsheet2 = ThisWorkbook.Sheets("Sheet2") lr1 = dbsheet1.Cells(Rows.Count, 1).End(xlUp).Row lr2 = dbsheet2.Cells(Rows.Count, 1).End(xlUp).Row For x = 2 To lr1 act1 = dbsheet1.Cells(x, 1) For y = 2 To lr2 act2 = dbsheet2.Cells(y, 1) If Not dbsheet2.Cells(y, 3).Value = "Match" Then 'Only compare if previoulsy not done or resulted in "No match" If act2 = act1 Then dbsheet2.Cells(y, 3).Value = "Match" If dbsheet2.Cells(y, 3).Value = "Match" Then For i = 2 To lr1 If dbsheet2.Cells(y, 1).Value = dbsheet2.Cells(i, 1).Value Then dbsheet2.Cells(y, 4).Value = "Match" Else dbsheet2.Cells(y, 4).Value = "No match" End If Next i End If Else dbsheet2.Cells(y, 3).Value = "No match" End If End If Next y Next x End Sub 

正如Nathan_Sav指出的,你可以用比赛公式解决你的问题。

这个MATCH公式让你在没有vba的情况下完全工作。

匹配col1和col2的公式:

 =IFERROR(IF(MATCH(sheet1!A1;sheet2!$A$1:$A$10;0)>=0;"Match");"No match") 

匹配公式返回find匹配的索引。 否则一个错误。 要得到“匹配”和“不匹配”这两个词,我们需要IFIFERROR公式。

col1&val1和col2&val 2的匹配公式

 {=IFERROR(IF(MATCH(sheet1!A1&sheet1!B1;sheet2!$A$1:$A$10&sheet2!$B$1:$B$10;0)>=0;"Match");"No match")} 

结合sheet2的两列使得使用数组公式是必要的。 为了使其工作,请按Ctrl + Shift + Enter。

我希望这有帮助