简体   繁体   中英

Excel VBA look for values in other sheet and copy them

I have two excel sheets: "Sheet1" and "Sheet2".

Sheet1 contains 3 columns with an N number of rows. Example:

x     y     result

A     b
B     m
L     a
A     b
B     b

Sheet2 contains 3 columns as well but with the result as an explanation for each x and y combination.

Example:

x     y      result

A     a        1
A     b        2
A     c        3
B     a        4

Please note that A != a, and result is not always a numeric value.

So basically I need to search Sheet2 for a given combination from values of Sheet1 and copy the result from Sheet2 to Sheet1.

Can you give me example of VBA code how to achieve this? Probably It's even possible with an Excel formula? Probably INDEX and MATCH? Anyhow, I can't figure this out by myself.

Thanks

First add another column containing a formula to create a unique key:

Sheet1:
   A       B       C       D
1  x       y       result  key 
2  A       b               =A2&B2
3  B       m               =A3&B3
4  L       a               =A4&B4
etc...

Sheet2:
   A       B       C       D     
1  x       y       result  key
2  A       a       1       =A2&B2
3  A       b       2       =A3&B3
4  A       c       3       =A4&B4
etc...

Then try this:

Sub FindResult()

Dim XY As String
Dim S1 As Object, S2 As Object
Dim ResultCell As Range, ResultValue As String

    Set S1 = Worksheets("Sheet1")
    Set S2 = Worksheets("Sheet2")

    Calculate
    For Rr = 2 To 6
        XY = S1.Cells(Rr, 4).Value
        Set ResultCell = S2.Range("D:D").Find( _
                                              What:=XY, _
                                              After:=S2.Range("D1"), _
                                              LookIn:=xlValues, _
                                              LookAt:=xlWhole, _
                                              SearchOrder:=xlByRows, _
                                              SearchDirection:=xlNext, _
                                              MatchCase:=True, _
                                              SearchFormat:=False _
                                             )
        If ResultCell Is Nothing Then
            ResultValue = "Not found"
        Else
            ResultValue = ResultCell.Offset(0, -1).Value
        End If
        S1.Cells(Rr, 3) = ResultValue
    Next Rr

End Sub

You can do this using formula itself. In your sheet1, please paste the below formula in C2 cell.

=IF(SUMPRODUCT((--EXACT(Sheet2!A:A,A2))*(--EXACT(Sheet2!B:B,B2))*(--(Sheet2!A:A<>""))*(--(Sheet2!B:B<>"")),ROW(Sheet2!A:A))=0,"",INDEX(Sheet2!C:C,SUMPRODUCT((--EXACT(Sheet2!A:A,A2))*(--EXACT(Sheet2!B:B,B2))*(--(Sheet2!A:A<>""))*(--(Sheet2!B:B<>"")),ROW(Sheet2!A:A))))

and copy the same to other cells. It would work.

Please check the below images:

表 1

表 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM