我们如何才能在sheet2中使用部分匹配Vlookup Sheet1中的数据Columns C

在Excel电子表格中可以做这个简单匹配/部分匹配function的VBAmacros/代码。

我有一个Excel工作簿2张。

Sheet1包含
ColumnA =名字
ColumnB =姓氏
ColumnC =职位

Sheet2包含
ColumnA =名字
ColumnB =姓氏
ColumnC =职位
ColumnD =电子邮件

我希望macros查看/匹配Sheet1 ColumnA,B,C与Sheet2 ColumnA,B,C和获取Sheet2 ColumnD数据到Sheet1列D与各自的行匹配。

注意:
在查找/匹配/部分匹配时,数据可能区分大小写。
必须做相应行的Sheet1和Sheet2“C”列的部分匹配

下面是附加的文件示例和运行macros后的结果应该看。

示例和结果文件我通过这些post,但没有find答案。

如何在excel中从sheet1中获取sheet2中的数据

如何使用Excel中的条件将sheet1中的数据复制到sheet2

合并r中部分匹配的数据

Excel VBA – 在工作表2中search工作表1中的值,并使用工作表1中的相邻值进行更新

你可以尝试一个FOR循环来比较这些值:

Sub CompleteData() Dim lastrow1 As Long, lastrow2 As Long Dim ws1 As Worksheet, ws2 As Worksheet Set ws1 = Worksheets("Sheet1") Set ws2 = Worksheets("Sheet2") lastrow1 = ws1.Cells(Rows.Count, "A").End(xlUp).Row lastrow2 = ws2.Cells(Rows.Count, "A").End(xlUp).Row For x = 2 To lastrow1 'change to 1 if you have no headers For y = 2 To lastrow2 'change to 1 if you have no headers If ws1.Cells(x, 1).Value = ws2.Cells(y, 1).Value And ws1.Cells(x, 2).Value = ws2.Cells(y, 2).Value And ws1.Cells(x, 3).Value = ws2.Cells(y, 3).Value Then ws1.Cells(x, 4).Value = ws2.Cells(y, 4).Value Exit For End If Next y Next x End Sub 

您可以使用AutoFilter()并将每个Sheet1行的对应值过滤到Sheet2列A到C:

 Option Explicit Sub CompleteData() Dim myRng As Range, cell As Range With Worksheets("Sheet1") Set myRng = .Range("A2", .cells(.Rows.Count, 1).End(xlUp)).SpecialCells(xlCellTypeConstants, xlTextValues) End With With Worksheets("Sheet2") With .Range("C1", .cells(.Rows.Count, 1).End(xlUp)) For Each cell In myRng .AutoFilter Field:=1, Criteria1:=cell.Value .AutoFilter Field:=2, Criteria1:=cell.Offset(, 1).Value .AutoFilter Field:=3, Criteria1:=cell.Offset(, 2).Value If Application.WorksheetFunction.Subtotal(103, .cells) > 1 Then cell.Offset(, 3).Value = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).cells(1, 4).Value .Parent.AutoFilterMode = False Next End With .AutoFilterMode = False End With End Sub