Vba 我们如何在sheet2中使用部分匹配列C来查找Sheet1数据
正在Excel电子表格中查找可以执行此简单匹配/部分匹配功能的VBA宏/代码。Vba 我们如何在sheet2中使用部分匹配列C来查找Sheet1数据,vba,excel,Vba,Excel,正在Excel电子表格中查找可以执行此简单匹配/部分匹配功能的VBA宏/代码。 我在excel工作簿中有两张工作表。 表1包含 ColumnA=名字 ColumnB=姓氏 ColumnC=职务 表2包含 ColumnA=名字 ColumnB=姓氏 ColumnC=职务 ColumnD=电子邮件 我希望宏将Sheet1 ColumnA,B,C与Sheet2 ColumnA,B,C进行vlookup/匹配 并将Sheet2 ColumnD数据放入Sheet1 ColumnD中,并匹配相应的行 注:
我在excel工作簿中有两张工作表。
表1包含
ColumnA=名字
ColumnB=姓氏
ColumnC=职务
表2包含
ColumnA=名字
ColumnB=姓氏
ColumnC=职务
ColumnD=电子邮件 我希望宏将Sheet1 ColumnA,B,C与Sheet2 ColumnA,B,C进行vlookup/匹配 并将Sheet2 ColumnD数据放入Sheet1 ColumnD中,并匹配相应的行 注:
执行vlookup/匹配/部分匹配时,数据可能区分大小写。
必须将Sheet1和Sheet2“C”列与相应行进行部分匹配 下面是运行宏后应查看的附件示例和结果 我浏览了这些帖子,但没有找到答案
您可以尝试使用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
您可以尝试使用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()
和filter Sheet2列A到C以及每个Sheet1行的相应值:
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
您可以使用
AutoFilter()
和filter Sheet2列A到C以及每个Sheet1行的相应值:
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
因此,表1中的职位可能比表2中的职位短,但永远不会比表2中的职位长?因此表1中的职位可能比表2中的职位短,但永远不会比表2中的职位长?它不会进行任何“部分匹配”。它只做精确匹配。有时,B列“melvin”将“melvin CPA”,但它不进行任何“部分匹配”。它只做精确匹配。有时,B栏的“梅尔文”将是“梅尔文CPA”