比较2个不同的Excel工作表中的文本并突出显示颜色

时间:2018-08-14 16:02:51

标签: vba excel-vba

Sub Compare()
Dim wS As Worksheet, wT As Worksheet
Dim r1 As Range, r2 As Range, r3 As Range
Dim cel1 As Range, cel2 As Range, cel3 As Range
Dim count As Integer      
Set wS = ThisWorkbook.Worksheets("Sheet1")
Set wT = ThisWorkbook.Worksheets("Sheet2")    
With wS
Set r1 = .Range("A1", .Cells(.Rows.count, .Columns("A:A").Column).End(xlUp))
End With    
With wT
Set r2 = .Range("A1", .Cells(.Rows.count, .Columns("A:A").Column).End(xlUp))
Set r3 = .Range("B1", .Cells(.Rows.count, .Columns("B:B").Column).End(xlUp))
End With    
On Error Resume Next       
For Each cel1 In r1
With Application
Set cel2 = .Index(r2, .Match(cel1.Value, r2, 0))   'find match in sheet2'
If Err = 0 The           
count = count + 1
cel1.Interior.Color = vbGreen '(color: font highlight)'
End If
Err.Clear
End With
Next cel1
MsgBox count & " Matched TF's are highlighted in Green Color", vbInformation     
End Sub

工作表Sheet1中的COL-A中的文本与工作表Sheet2中的COL-A进行文本比较。 但是我也想检查工作表2中COL-B中的文本。当sheet1和sheet2中的COL-A匹配时,应检查COL-B中的文本是否为“ Ok”,然后颜色突出显示。我该怎么办?

sheet1

enter image description here

sheet2

enter image description here

结果

enter image description here

0 个答案:

没有答案