简体   繁体   English

用于比较和突出显示区分大小写的数据的宏

[英]Macro to compare and highlight case-sensitive data

I came across a macro that compares data pasted in column B with column A and highlights column B if not an Exact match with column A. 我遇到了一个宏,该宏将B列中的数据与A列中的数据进行比较,并突出显示B列(如果与A列不完全匹配)。

Sub HighlightNoMatch()
    Dim r As Long
    Dim m As Long
    m = Range("B" & Rows.Count).End(xlUp).Row
    Range("B1:B" & m).Interior.ColorIndex = xlColorIndexNone
    For r = 1 To m
        If Evaluate("ISERROR(MATCH(TRUE,EXACT(B" & r & ",$A$1:$A$30),0))") Then
            Range("B" & r).Interior.Color = vbRed
        End If
    Next r
End Sub

How do I change the code to achieve as below - 如何更改代码以实现以下目标-

I want the code to highlight Column F on sheet2, if it is not an exact match with data in Column B on sheet1." 我希望代码突出显示sheet2的F列,如果它与sheet1的B列中的数据不完全匹配。”

Rather than having a fixed range ($A$1:$A$30) I would loop through each value in the range and check for a match: 而不是使用固定范围($ A $ 1:$ A $ 30),我将遍历范围中的每个值并检查是否匹配:

    Sub HighlightNoMatch()
        Dim t As Long
        Dim m As Long

        m = Worksheets("Sheet2").Range("F" & Rows.Count).End(xlUp).Row
        t = Worksheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row

        Worksheets("Sheet2").Range("F1:F" & m).Interior.ColorIndex = xlColorIndexNone
        For x1 = 1 To m
            For x2 = 1 To t
                If Worksheets("Sheet2").Range("F" & x1).Value = Worksheets("Sheet1").Range("B" & x2).Value Then
                    Exit For
                ElseIf Worksheets("Sheet2").Range("F" & x1).Value <> Worksheets("Sheet1").Range("B" & x2).Value And x2 = t Then
                    Worksheets("Sheet2").Range("F" & x1).Interior.Color = vbRed
                End If
            Next x2
        Next x1
    End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM