Comparing two columns in one Excel sheet, to two columns in another sheet, and if they match, copy data from another column

后端 未结 4 1551
死守一世寂寞
死守一世寂寞 2021-01-29 09:27

I\'ve been looking at using the Excel VLOOKUP function to accomplish this, but I\'m pretty unfamiliar with it.

I need to do the following:

On sheet one, column A

相关标签:
4条回答
  • 2021-01-29 09:43

    I would suggest a VBA script that uses an SQL query, maybe something like this (you need some SQL language skills in order to get the right result). Anyways, this is the approach I would recommend for an advanced user:

    Dim oConn As ADODB.Connection, rs As ADODB.Recordset
    
    sWorkbookName = ThisWorkbook.FullName
    connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=""" & 
    sWorkbookName & """;Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX = 1"""
    
    sSheet1="myDataSheet1"
    sSheet2="myDataSheet2"
    oConn.Open connString
    'just an example of SQL, you have to customize it
    sSQL = "SELECT [FIRST NAME], [LAST NAME],[EMAIL] FROM [" & sSheet2 & "$] " & 
    " WHERE 
    [FIRST NAME] + [LAST NAME} IN (SELECT [FIRST NAME] + [LAST NAME] FROM [" & sSheet1 & "$]" & ")  ORDER BY [FIRST NAME] ASC"
    
    rs.Open sSQL, oConn, adOpenStatic, adLockOptimistic, adCmdText
    
    'dump results on a temporary sheet 
    ThisWorkbook.Worksheets("tmp_sheet").Range("A2").CopyFromRecordset rs
    
    rs.Close
    oConn.Close  
    
    0 讨论(0)
  • 2021-01-29 09:59
    =IF(AND(Sheet1.A1=Sheet2.A1, Sheet1.B1=Sheet2.B1),Sheet2.C1,'')
    

    Save this as formula to the Column C of the target sheet.

    0 讨论(0)
  • 2021-01-29 10:07

    You can enter in Sheet1!C1

    =INDEX(Sheet2!C:C,SUMPRODUCT(--(Sheet2!A:A=A1),--(Sheet2!B:B=B1),ROW(Sheet2!C:C)),0)
    

    and then copy downwards.

    This does not require:

    1. Using VBA
    2. Using array formulas
    3. Using an additional (helper) column.

    The importance of this is out of scope here.

    More details

    What you are looking for is typically called Multiple Lookup. There are quite a few questions about it in SO, and many other articles elsewhere. I have compiled here a list of such posts.

    There are many possible solutions for that. The one I found most robust is shown here. This is what I used in the present answer.

    0 讨论(0)
  • 2021-01-29 10:08

    try this:

    Put this formula on sheet1 column C:

    =VLOOKUP(CONCAT(A1,B1),Sheet2!A:D,4,0)
    

    You would need to have 4 columns on sheet2, the first column would need to be a CONCATENATE FORMULA like this:

    =CONCAT(B1,C1)
    

    The second column would be your first name, third column your last name and last column the corresponding email.

    How this formula work's?

     =VLOOKUP(**CONCAT(A1,B1)**,Sheet2!A:D,4,0)
    

    It's concatenating the first and last name on sheet1 and looking for it on Sheet2 on column A, if there's a match it will return the email cell value in sheet2 column D (column D index is 4).

    Hope this help you

    0 讨论(0)
提交回复
热议问题