Comparing columns in two different excel sheets and workbooks, then posting the matching value into another column

那年仲夏 提交于 2019-12-05 06:51:32

Sounds like you could accomplish your goal with the VLookup function. Add this formula to Sheet1 F1: =IFERROR(VLookup(E1,Sheet2!A:D,4,FALSE),"")

That formula will copy the cell into sheet1 if a match is found, but if no match is found the cell will remain blank.

Try to use the below code. I have just modified your code using StrComp function

Sub FindMatches()

    Dim oldRow As Integer
    Dim newRow As Integer
    Dim i As Integer
    i = 1
    For oldRow = 1 To 1170
        For newRow = 1 To 1170
            If StrComp((Worksheets("Sheet1").Cells(oldRow, 5).Text), (Worksheets("Sheet2").Cells(newRow, 1).Text), vbTextCompare) <> 0 Then
                i = oldRow
                Worksheets("Sheet1").Cells(i, 6) = " "
                Else
                Worksheets("Sheet1").Cells(i, 6) = Worksheets("Sheet2").Cells(newRow, 4)
                i = i + 1
                Exit For
            End If
        Next newRow
    Next oldRow

End Sub
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!