Search code examples
excelexcel-2007vba

Get value of one column based on value of two OTHER columns, then change column on another worksheet


I have a workbook with three worksheets. I need to change the value in column K(row) on workbook B with the value of column CF(row) from workbook A based on a criteria (index?) of columns A and C on Workbook A and columns B and D on worksheet B. That is, these worksheets have the data in a DIFFERENT ORDER, one cannot simply loop through and copy over. I need to find the Row number of the CF(row) in worksheet A where the worksheet A and C columns are the same as the worksheet A B and D columns.

PsuedoSQL

UPDATE 
    Worksheet B
SET
    Worksheet B.K = Worksheet A.CF
FROM
    Worksheet A
WHERE
    Worksheet A.B = Worksheet B.B

Select from Worksheet A, column CF WHERE A.A AND A.C is equal to worksheet B.B and B.D AND SET Worksheet B.K(Row) to Value worksheetA.CF(row)

The code I have is a look up that finds the LAST column with the matching text on worksheet A Column A, without any index or mapping.

Public Function FindPnum(keyword As Variant) As Variant
    Dim ws As Worksheet
    Dim rng1 As Range
    Dim rng2 As Range
    Dim tRow As Long
    Dim tCol As Integer

    Set ws = Sheets("Data")
    Set rng1 = ws.Cells.Find(keyword, ws.[b2], xlValues, , xlByRows, xlPrevious)
    Set rng2 = ws.Cells.Find(keyword, ws.[b2], xlValues, , xlByColumns, xlPrevious)

    If Not rng1 Is Nothing Then
        FindPnum = Cells(rng1.Row, rng2.Column).Address(0, 0)
    End If

End Function

Solution

  • Here try this

    Sub pair()
    Dim r, r2, found
    r = 8
    r2 = 2
    found = False
    Application.ScreenUpdating = False
    Sheets(2).Activate
    Do Until Len(Cells(r, 2).Value) = 0
    Do Until Len(Sheets(1).Cells(r2 + 1, 2).Value) = 0 Or found = True
        If Sheets(2).Cells(r, 2).Text = Sheets(1).Cells(r2, 2).Text Then
            If Sheets(2).Cells(r, 11).Value <> 0 Then
                Sheets(2).Cells(r, 11).Value = Sheets(1).Cells(r2, 84)
            End If
            found = True
        Else
            r2 = r2 + 1
        End If
    Loop
    r2 = 2
    r = r + 1
    found = False
    Loop
    Application.ScreenUpdating = True
    End Sub
    

    I think this should do it.