【问题标题】:How can we Vlookup Sheet1 data in sheet2 with partial match Columns C我们如何使用部分匹配列 C 在 sheet2 中查找 Sheet1 数据
【发布时间】:2017-08-05 15:58:02
【问题描述】:

寻找可以在 Excel 电子表格中执行此简单匹配/部分匹配功能的 VBA 宏/代码。

我在 Excel 工作簿中有 2 张工作表。

Sheet1 包含
ColumnA = 名字
列 B = 姓
C 列 = 职位

Sheet2 包含
ColumnA = 名字
列 B = 姓
C 列 = 职位
ColumnD = 电子邮件

我希望宏将 Sheet1 ColumnA、B、C 与 Sheet2 ColumnA、B、C 进行查找/匹配 并将 Sheet2 ColumnD 数据放入 Sheet1 columnD 中,并匹配相应的行。

注意:
在进行 vlookup/match/partial match 时,数据可能区分大小写。
必须对 Sheet1 和 Sheet2 “C”列与各自的行进行部分匹配

以下是附加文件示例,运行宏后应显示结果。

Sample and results files 我浏览了这些帖子,但没有找到答案。

how to get data in sheet2 from sheet1 in excel

How to copy data from sheet1 to sheet2 with a condition in Excel

merge data with partial match in r

Excel VBA - Search for value from sheet1 in sheet2 and update with adjacent value from sheet1

【问题讨论】:

  • 所以 Sheet1 中的职位名称可能比 Sheet2 中的短,但永远不会长?

标签: vba excel


【解决方案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

【讨论】:

  • 它不做任何“部分匹配”。它只会完全匹配。有时, columnb "melvin" 将 "melvin CPA" Samplefile
【解决方案2】:

您可以使用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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-25
    相关资源
    最近更新 更多