首页 > 解决方案 > 使用 VBA 替代 Vlookup。比较列中的值并将相应值从第二列复制到另一列

问题描述

我有一个任务,我不想使用vlookup,因为它使过程非常缓慢。我正在为该任务寻找一个纯粹的 VBA 代码解决方案。

在这里,我将工作表 2 中 A 列和 D 列中的值合并。如果工作表 1 的 A 和 B 列中的值与工作表 2 中的值相同,那么我将工作表 2 中 G 列中的相应值复制到 D 列表 1。

  Application.ScreenUpdating = False

    Sheets("Sending List").Select

    Dim Lastrow1, Lastrow2 As Long
    Dim ws1, ws2 As Worksheet
    Dim tempVal, tempVal2 As String

    Set ws1 = Sheets("Sending List")
    Set ws2 = Sheets("P13 D-Chain Status")

    Lastrow1 = ws1.Range("B" & Rows.Count).End(xlUp).Row
    Lastrow2 = ws2.Range("B" & Rows.Count).End(xlUp).Row

    With ws2
        .Range("Q2:Q" & Lastrow2).Formula = "=A2&D2"
        .Range("R2:R" & Lastrow2).Formula = "=G2"
    End With

    With ws1
        .Range("D2:D" & Lastrow1).Formula = "=VLOOKUP(A2&B2,'P13 D-Chain Status'!Q:R,2,0)"
    End With



  Application.ScreenUpdating = True

End Sub

标签: excelvbavlookup

解决方案


试试这个:

=INDEX('P13 D-Chain 状态'!Q:R,MATCH(A2&B2,'P13 D-Chain 状态'!Q:Q,0),2)

但我认为它不会工作得更快。

希望能帮助到你


推荐阅读