Excel VBA look for values in other sheet and copy them

为君一笑 提交于 2019-12-13 09:40:49

问题


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


回答1:


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:




回答2:


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


来源:https://stackoverflow.com/questions/39407963/excel-vba-look-for-values-in-other-sheet-and-copy-them

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