我有4列数据。它们是复制和粘贴在一起的名字/姓氏列的两个单独列。
我想要做的是对姓氏进行匹配,如果它们相等,则对名字进行匹配。
列范围是动态的,这就是为什么运行连接和VLOOKUP公式是有效的,但如果我能得到一些不那么复杂的东西,那就太好了。
A B C D E
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发布于 2013-10-05 06:01:46
@user2140261的评论+1 ...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测试的
这将根据H11检查F11。如果匹配,它会根据I11检查G11,如果返回匹配,则将"It's a match!"写入J11。如果不匹配,则将"Sorry, it's not a match"写入J11。然后,它开始执行第12行的循环。
发布于 2013-10-05 06:16:49
要做到这一点,似乎没有必要使用VBA。你可以在E2中加入这个数组公式(点击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!")
IF函数在两个条件都为真时赋值2,如果为假则赋值1。MAX将从值数组中找到最高值。CHOOSE将根据值返回短语。1=“无匹配”,2=“匹配”。
https://stackoverflow.com/questions/19190477
复制相似问题