Search code examples
excelexcel-formulaexcel-2007

Compare two Excel columns and if a match found paste the value of a third column into a fourth


If cell C2 value is in the range P2:P25 then paste the value in the matching row of Column T into the same row of Column N.

View here for image.


Solution

  • One way, written as a standalone example and assumes that data is on Sheet1 and transfers the first match. Note that there is no error checking/handling in this example.

    Sub xferNum()
    Dim ws As Worksheet
    Dim srow As Long, erow As Long, scol As Long, srchcol As Long
    Dim rsltcol As Long, lucol As Long
    Dim fndNo As Range, c As Range, lookrng As Range
    
    Set ws = Sheets("Sheet1")
    srow = 2
    scol = 3
    srchcol = 16
    lucol = 20
    rsltcol = 14
    
        With ws
            erow = .Cells(.Rows.Count, scol).End(xlUp).Row
            Set lookrng = .Range(.Cells(srow, scol), .Cells(erow, scol))
                For Each c In lookrng
                    Set fndNo = Columns(srchcol).Find(what:=c.Value)
                        If Not fndNo Is Nothing Then
                            .Cells(c.Row, rsltcol).Value = fndNo.Offset(0, lucol - fndNo.Column).Value
                        End If
                Next c
        End With
    End Sub