VBA Excel将A上的2列与B上的2列匹配并返回值

时间:2016-11-08 10:43:23

标签: excel vba compare match multiple-columns

我需要一个VBA Excel脚本来比较A列和A列。工作表一中的B对A& B在工作表中如果找到匹配,则返回两个返回工作表一中的C列。

我可以使用公式在excel中执行此操作,但它是速度即可,所以希望通过VBA做得更快,并且id更喜欢表的最终输出只包含值而不是公式。

我已经做了很多挖掘,但无法找到这个特殊要求。

非常感谢任何帮助。

这是我目前正在使用的excel公式

{= IFERROR(INDEX(SQLDATA d:d,MATCH(1,(SQLDATA一个:A = A2)*(SQLDATA B:!!! B = B2),0))," 0&#34 ;)}

1 个答案:

答案 0 :(得分:0)

Sub Stridhan()

Dim c As Range, d As Range, lr As Long
Dim ws1 As Worksheet, ws2 As Worksheet
Dim rng1 As Range, rng2 As Range

With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With

'rename Sheet1 and Sheet2
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("Sheet2")

Set rng1 = ws1.Range("A2", ws1.Range("A" & ws1.Cells(Rows.Count, 1).End(xlUp).Row))
Set rng2 = ws2.Range("A2", ws2.Range("A" & ws2.Cells(Rows.Count, 1).End(xlUp).Row))

With ws2
    lr = .Cells(Rows.Count, 3).End(xlUp).Row
    If lr > 1 Then .Range("C2", "C" & lr).ClearContents
End With

For Each c In rng2
For Each d In rng1
    If c = d Then
        If c.Offset(0, 1) = d.Offset(0, 1) Then
            c.Offset(0, 2).Value = d.Offset(0, 2).Value
            GoTo Nextone
        End If
    End If
Next d
Nextone:
Next c

With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With

End Sub