1

我有 4 列数据。它们是复制并粘贴在一起的名字/姓氏列的两个单独列。

我想要做的是对姓氏进行匹配,如果它们相等,则对名字进行匹配。

列范围是动态的,这就是运行 CONCATENATE 和 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
4

2 回答 2

0

+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

经过测试

这将检查F11. H11如果匹配,则检查G11. I11,如果返回匹配,"It's a match!"则写入J11. 如果不匹配,"Sorry, it's not a match"则写入J11. 然后它开始循环第 12 行。

于 2013-10-04T22:01:45.643 回答
0

为此,似乎没有必要使用 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!")

如果两个条件都为 TRUE,则 IF 函数赋值 2,如果为 FALSE,则赋值 1。MAX 将找到值数组中的最大值。CHOOSE 将根据值返回短语。1 =“不匹配”,2 =“匹配”。

于 2013-10-04T22:16:49.063 回答