VBLOOKUP和MATCH识别专栏

时间:2018-05-28 21:56:57

标签: excel vba excel-vba

为了使事情变得尽可能简单,我们假设我有3张纸。表1包含名称和团队。表2只需包含与特定团队相关的名称。但是,名称是手动输入的。如何检查工作表2以确保其上的所有名称都属于特定团队,并输出模糊性到工作表3?重要的是要注意每个不断变化的名单的侧面都有额外的列和行,这些差异在每个表中都不一致。

第1页

Seger, Bob          Team A
Hendrix, Jimi       Team B
Osbourne, Ozzy      Team C
Shepherd, Kenny     Team B
Rose, Axl           Team A
Keenan, Maynard     Team C

表2(仅限B队)

Hendrix, Jimi
Shepherd, Kenny
Rose, Axl
Keenan, Maynard

表3(不准确)

Rose, Axl
Keenan, Maynard

2 个答案:

答案 0 :(得分:1)

在sheet3中未使用的列的顶部尝试此操作,并填写以后的匹配项。

=INDEX(Sheet2!A:A, AGGREGATE(15, 6, ROW($2:$99)/NOT(COUNTIFS(Sheet1!A$2:A$99, Sheet2!A$2:A$99, Sheet1!B$2:B$99, "team b")), ROW(1:1)))

enter image description here

答案 1 :(得分:1)

您可以使用词典

Option Explicit
Public Sub VerifyNamesInSheet()

    Application.ScreenUpdating = False

    Const SHEET_TO_CHECK As String = "Sheet2"
    Const TEAM_TO_CHECK As String = "Team B"
    Dim teamNamesDict As Object, misplacedDict As Object, valuesToProcess()
    Set teamNamesDict = CreateObject("Scripting.Dictionary")
    Set misplacedDict = CreateObject("Scripting.Dictionary")

    With ThisWorkbook.Worksheets("Sheet1")
        valuesToProcess = .Range("A1:B" & .Cells(.Rows.Count, "A").End(xlUp).Row).Value
    End With

    Dim i As Long

    For i = LBound(valuesToProcess, 1) To UBound(valuesToProcess, 1)
        If valuesToProcess(i, 2) = TEAM_TO_CHECK Then
            If Not teamNamesDict.Exists(valuesToProcess(i, 1)) Then teamNamesDict.Add valuesToProcess(i, 1), valuesToProcess(i, 1)
        End If
    Next i

    With ThisWorkbook.Worksheets(SHEET_TO_CHECK)
        valuesToProcess = .Range("A1:A" & .Cells(.Rows.Count, "A").End(xlUp).Row).Value

        For i = LBound(valuesToProcess, 1) To UBound(valuesToProcess, 1)
            If Not teamNamesDict.Exists(valuesToProcess(i, 1)) And Not misplacedDict.Exists(valuesToProcess(i, 1)) Then misplacedDict.Add valuesToProcess(i, 1), valuesToProcess(i, 1)
        Next i

    End With

    With ThisWorkbook.Worksheets("Sheet3")
        .UsedRange.ClearContents
        .Range("A1").Resize(misplacedDict.Count, 1) = Application.WorksheetFunction.Transpose(misplacedDict.Keys)
    End With

    Application.ScreenUpdating = True
End Sub