【问题标题】:Highlighting mismatched cells using vba使用 vba 突出显示不匹配的单元格
【发布时间】:2018-01-29 10:23:46
【问题描述】:

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

我在同一个 Excel 工作簿中有工作表 2,列名 allowed phonetype 和每个单元格中的一些字符串。

现在我想比较 sheet 1Phonetype 列中的字符串是否与 sheet 2allowed 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

表格 2 中不存在字符串“heythere”(列:“允许的电话类型”),因此应突出显示

【问题讨论】:

  • 你做了什么?什么不工作?发布代码,您应该显示您尝试过的内容。请注意,类似的问题已经被问过很多次了,也有人回答过很多次,所以试试搜索功能吧。

标签: vba excel


【解决方案1】:

这里有一些东西可以帮助你开始

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

【讨论】:

    【解决方案2】:

    检查一下,

    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,

    【讨论】:

    • 在表 2 中,我有一个名为“允许的电话类型”的列,其中包含一个字符串列表,例如。单元格 1=abc,cell2=def 等。现在,在“phonetype”列下的工作表 1 中,单元格包含单元格 1=abc,cell 3=ghf 等字符串。如果工作表 1 中的字符串,我必须突出显示单元格不等于表 2 中的任何字符串。您能修改您的代码吗?
    猜你喜欢
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 2020-10-27
    相关资源
    最近更新 更多