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

时间:2015-05-26 10:15:33

标签: excel vba excel-vba

在excel表1中,我有一个名为 phonetype 的列,每个单元格中都有一些字符串。

我在同一个excel工作簿中有第2页,列名为允许的phonetype ,每个单元格中有一些字符串。

现在我要比较第1页Phonetype列中的字符串是否与第2页<{strong}的allowed phonetype列中的字符串相同>;如果没有突出显示那些细胞。

使用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

字符串&#34; heythere&#34;在表2中没有(列:&#34;允许的电话类型&#34;),因此应突出显示

2 个答案:

答案 0 :(得分:0)

这里有一些可以帮助您入门

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")

答案 1 :(得分:0)

检查出来,

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

Found here,