如何根据特定标准突出显示单元格

时间:2015-09-28 22:41:27

标签: excel vba excel-vba

我的表1中包含以下数据(数千条):

   column A         colum B    
1  Tag_Number       WBS      
2  61LV    -1502    6103    
3  61LV    -1508    6104    
4  61PDT   -1013    6002    
5  61PDT   -2943    6191

我的表2也有相同的信息,但数据的顺序可能不一样:

   column A         colum B
1  Tag_Number       WBS  
2  61LV    -1508    6104
3  61LV    -1502    6103
4  61PDT   -1013    6002
5  61PDT   -2943    6191

如何将表2中的B3强调为黄色,因为表2 61LV -1502 6103的数据与61LV -1502 WBS 6103不同?

1 个答案:

答案 0 :(得分:0)

Sub MyMacro()
    Dim val, cell As Range, rows1 As Long, rows2 As Long
    rows1 = Sheet1.Range("A32767").End(xlUp).Row
    rows2 = Sheet2.Range("A32767").End(xlUp).Row
    Sheet2.Activate
    For Each cell In Sheet2.Range("A2:A" & rows2).Cells
        val = Application.IfError(Application.VLookup(cell, Sheet1.Range("A2:B" & rows1), 2, False), "")
        If val = "" Then
            MsgBox "Tag not found: " & cell.Value
            cell.Select
            Exit For
        ElseIf cell.Offset(, 1).Value <> val Then
            cell.Offset(, 1).Select ' or change interior color
            Exit For ' or dont exit if you want to color all unmatched cells
       End If
    Next cell
End Sub