【问题标题】:VBA Excel To Match 2 columns on A with 2 columns on B and return valueVBA Excel将A上的2列与B上的2列匹配并返回值
【发布时间】:2016-11-08 10:43:23
【问题描述】:

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

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

我做了很多挖掘,但找不到这个特殊的要求。

感谢您对此提供任何帮助。

这是我目前使用的 excel 公式

{=IFERROR(INDEX(SQLData!D:D,MATCH(1,(SQLData!A:A=A2)*(SQLData!B:B=B2),0)),"0")}

【问题讨论】:

标签: excel vba compare match multiple-columns


【解决方案1】:
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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    相关资源
    最近更新 更多