在 2 张纸之间查找

Lookup between 2 sheets

我想弄清楚 Sheet1 中特定范围内的哪些项目写在 sheet 2 的 A 列中。 如果该项目在两个 sheet 中 - 它将在 sheet1.

中着色

目前代码几乎可以正常工作,但它会为所有项目着色,并且应该只为出现在两个 sheet 中的项目着色。

到目前为止我尝试了:

Sub Sample()

    Dim cell As Range
    Dim Result

    For Each cell In Worksheets("Sheet1").Range("D2:H9")
        On Error Resume Next
        Result = Application.XLookup(cell, Worksheets("Sheet2").Range("A:A"), 1, True)
        'On Error GoTo 0

       If Not IsEmpty(cell) Then
            'If cell = Result Then
                cell.Font.Bold = True
                cell.Interior.ColorIndex = 4
                cell.Interior.Pattern = xlSolid
            'End If
            Result = ""
        End If
        
        'If cell = "Error 2042" Then
            'nothing found
        'ElseIf cell = Result Then
            'cell.Interior.ColorIndex = 4
        'End If
        
    Next
    
    On Error GoTo 0

End Sub

提前致谢。

试试这个。普通的Match就可以了,不知道XMATCH有什么优势

您也可以使用条件格式(有或没有 VBA)来做到这一点。

Sub Sample()

Dim cell As Range
Dim Result

For Each cell In Worksheets("Sheet1").Range("D2:H9")
    Result = Application.XMatch(cell, Worksheets("Sheet2").Range("A:A"), 0)
    If Isnumeric(Result) Then 'no match
        cell.Font.Bold = True
        cell.Interior.ColorIndex = 4
        cell.Interior.Pattern = xlSolid
    End If
Next
    
End Sub