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

后端 未结 4 1552
死守一世寂寞
死守一世寂寞 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条回答
  •  梦毁少年i
    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  
    

提交回复
热议问题