在Excel VBA中运行嵌套的If / Match

嘿,我花了大部分时间来试图解决这个问题 – 我觉得我非常接近。 基本前提是我有4列数据。 它们是复制并粘贴在一起的两个单独的名字/姓氏列。 现在我想要做的就是在姓氏上运行一个匹配,如果他们是平等的,在名字上运行匹配。 列范围是dynamic的,这就是为什么运行一个CONCATENATE和VLOOKUP公式的原因,但如果我能得到一些较less的参与,那就太好了。

Here's a sample data table ABCDE 1 Last First Last2 First2 2 Sharma Abhi Smith Kevin 3 Philip Matt Smith GEORGIA 4 Franc Pete John Bon Jovi 5 Arnold Susan Jack White 6 Mallo Chad Sharma Katie 7 Daigle Steve Sharma Abhi 

而我的想法是,从单元格E2开始,它应该返回一个匹配或不匹配(在这种情况下,只有第2行应该返回一个匹配,目前它每次都返回一个匹配 – 这是不正确的,我觉得我失踪小东西?感谢您的帮助。

这是迄今为止我写的代码

 Sub matchFunction() On Error Resume Next Dim BW_Row As Long Dim BW_Clm As Long Table1 = Sheet2.Range("F11:F16") ' Range of all appointments last name Table2 = Sheet2.Range("$G$11:$G$16") ' Range of all appointments first name Table3 = Sheet2.Range("$H$11:$H$16") ' Range of leads BW_Row = Sheet2.Range("J11").Row ' Change E column if it's a match BW_Clm = Sheet2.Range("J11").Column For Each c In Table1 For Each d In Table2 If Application.Match(c, Table3, 0) <> "" Then If Application.Match(d, Table3, 0) <> "" Then Sheet2.Cells(BW_Row, BW_Clm) = "It's a Match!" Else Sheet2.Cells(BW_Row, BW_Clm) = "Sorry, it's not a match" End If End If BW_Row = BW_Row + 1 Next d Next c MsgBox "Matching Finished" End Sub 

+1对@ user2140261的评论… VBA将比你的公式慢。 但是,如果您使用VBA,请插入此代替For Each C循环:

 i = 11 For Each c In Table1 If c = Cells(i, 8) Then If Cells(i, 7) = Cells(i, 9) Then sheet2.Cells(i, BW_Clm) = "It's a Match!" End If sheet2.Cells(i, BW_Clm) = "Sorry, it's not a match" i = i + 1 Next c 

经testing

这将检查F11H11 。 如果是匹配,则检查G11I11 ,如果返回匹配"It's a match!" 写给J11 。 如果不匹配,则写给J11 "Sorry, it's not a match" 。 然后它开始循环第12行。

使用VBA似乎没有必要这样做。 你可以在E2中input这个数组公式(按Ctrl + Shift + Enter):

=CHOOSE(MAX(IF(($C$2:$C$7=$A2)*($D$2:$D$7=$B2),2,1)),"Sorry, it's not a match","It's a Match!")

如果两个条件均为TRUE,则IF函数将赋值2,如果FALSE则为1。 MAX将从值数组中find最高值。 CHOOSE将根据该值返回短语。 1 =“不匹配”,2 =“匹配”。