使用vba突出显示不匹配的单元格

在Excel表1中,我有一个名为phonetype的列,每个单元格中都有一些string。

我有同一个Excel工作簿中的表2 允许列名允许phonetype和每个单元格中的一些string。

现在我想比较表1的 Phonetype列中的stringallowed phonetype表2的 allowed phonetype列中的string相同; 如果不突出显示这些单元格

一切都使用vba。

  Sheet 1 Sheet 2 column name:"Phonetype" columnname:"allowed phone type" cell 1:welcome cell 1:welcome cell 2: cell 2:hi121 cell 3:heythere cell 4:hi121 

在表2中不存在string“heythere”(列:“允许的电话types”),所以应该突出显示

这里有一些让你开始

 Option Explicit '// Campare and Hilight Unique Sub CompareHighlightUnique() Dim Range1 As Range Dim Range2 As Range Dim i As Integer Dim j As Integer Dim isMatch As Boolean For i = 2 To Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row isMatch = False Set Range1 = Sheets("Sheet1").Range("A" & i) For j = 1 To Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row Set Range2 = Sheets("Sheet2").Range("A" & j) If StrComp(Trim(Range1.Text), Trim(Range2.Text), vbTextCompare) = 0 Then isMatch = True Exit For End If Set Range2 = Nothing Next j If Not isMatch Then Range1.Interior.Color = RGB(255, 0, 0) End If Set Range1 = Nothing Next i End Sub 

要更改高亮颜色编辑RGB(255, 0, 0)

更改sheet1或sheet2编辑("Sheet1") and ("Sheet2")

一探究竟,,

 Sub Button1_Click() Dim ws As Worksheet, sh As Worksheet Dim Rws As Long, Rng As Range, a As Range Dim Rws2 As Long, rng2 As Range, c As Range Set ws = Sheets("Sheet1") Set sh = Sheets("Sheet2") With ws Rws = .Cells(.Rows.Count, "A").End(xlUp).Row Set Rng = Range(.Cells(2, 1), .Cells(Rws, 1)) Rng.Interior.ColorIndex = 6 End With With sh Rws2 = .Cells(.Rows.Count, "A").End(xlUp).Row Set rng2 = Range(.Cells(2, 1), .Cells(Rws2, 1)) End With For Each a In Rng.Cells For Each c In rng2.Cells If a = c Then a.Interior.Color = xlNone Next c Next a End Sub 

在这里find,