查找匹配项并从同一行获取数据

时间:2019-06-06 00:41:39

标签: excel vba

我正在使用这段代码来查找A column value(sheet1)E column value (sheet2)之间的匹配项。如果存在匹配项,则从该行中获取值,然后将该值放入sheet2中。该代码运行良好且速度更快。但是,尽管A column value(sheet1)E column value (sheet2)之间(尤其是列中间的数据)匹配,但我仍然面临这个奇怪的问题。

Dim wsDest1 As Worksheet
Set wsDest1 = ThisWorkbook.Worksheets("sheet2")

Dim wsLookup1 As Worksheet
Set wsLookup1 = ThisWorkbook.Worksheets("sheet1")

Dim lasRow1 As Long
lasRow1 = wsDest1.Cells(wsDest1.Rows.Count, "E").End(xlUp).Row

Dim MatchedRow1 As Double

Dim iRow1 As Long
For iRow1 = 2 To lasRow1
    MatchedRow1 = 0 'initialize!
    On Error Resume Next
    MatchedRow1 = Application.WorksheetFunction.Match(wsDest1.Cells(iRow1, "E").Value, wsLookup1.Columns("A"), 1)
    On Error GoTo 0

    If MatchedRow1 <> 0 Then
        If wsDest1.Cells(iRow1, "E").Value = wsLookup1.Cells(MatchedRow1, "A").Value Then
            wsDest1.Cells(iRow1, "A").Value = wsLookup1.Cells(MatchedRow1, "J").Value
            wsDest1.Cells(iRow1, "B").Value = wsLookup1.Cells(MatchedRow1, "H").Value
            wsDest1.Cells(iRow1, "C").Value = wsLookup1.Cells(MatchedRow1, "G").Value
            wsDest1.Cells(iRow1, "D").Value = wsLookup1.Cells(MatchedRow1, "N").Value
            wsDest1.Cells(iRow1, "F").Value = wsLookup1.Cells(MatchedRow1, "D").Value
        End If
    End If
Next iRow1

0 个答案:

没有答案