简体   繁体   中英

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

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 and column B contain 3000 rows of first names and last names.

I need to compare this to a second sheet that also has first names and last names, with a third column containing email addresses.

If the two columns are an exact match on sheet 1 and sheet 2(eg A1+B1 = Sheet2 A7+B7), I need to copy the email address from column C on sheet 2, to column C on sheet 1.

Is there a VLOOKUP formula to accomplish this, or is this going to need to be a VBA script?

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

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  

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.

=IF(AND(Sheet1.A1=Sheet2.A1, Sheet1.B1=Sheet2.B1),Sheet2.C1,'')

将此公式保存为目标工作表的C列。

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