比较两个文本单元格并显示Excel表格2010/2007中第三列的差异

时间:2013-02-17 07:32:43

标签: excel

我想比较两个包含文本的单元格并显示某种颜色的差异

Ex:Cell A:NTR is a great Leader.
   Cell B:Ntr is a Great leader

2 个答案:

答案 0 :(得分:3)

使用VBA .....

Sub CompareInColor()

ActiveSheet.Range("C1").Value = ActiveSheet.Range("A1").Value

For i = 1 To Len(ActiveSheet.Range("A1").Value)
    If (ActiveSheet.Range("A1").Characters(i, 1).Text <> 
        ActiveSheet.Range("B1").Characters(i, 1).Text) Then 
        ActiveSheet.Range("C1").Characters(i, 1).Font.Color = RGB(255, 0, 0)
Next i

End Sub

仅比较A1和B1 ...如果您有很多行,则循环遍历您的行。另外,我假设A1和A2的长度相同,否则可能会出现超出范围的索引错误。

答案 1 :(得分:0)

Sub CompareInColor()

ActiveSheet.Range("C1").Value = ActiveSheet.Range("A1").Value

For i = 1 To Len(ActiveSheet.Range("A1").Value)
    If (ActiveSheet.Range("A1").Characters(i, 1).Text <> ActiveSheet.Range("B1").Characters(i, 1).Text) Then
        ActiveSheet.Range("C1").Characters(i, 1).Font.Color = RGB(255, 0, 0)
End If
Next i

End Sub

Sub CompareInColorFlip()

ActiveSheet.Range("D1").Value = ActiveSheet.Range("B1").Value

For i = 1 To Len(ActiveSheet.Range("B1").Value)
    If (ActiveSheet.Range("B1").Characters(i, 1).Text <> ActiveSheet.Range("A1").Characters(i, 1).Text) Then
        ActiveSheet.Range("D1").Characters(i, 1).Font.Color = RGB(255, 0, 0)
End If
Next i

End Sub

比较A到B然后将C和B中差异的字体更改为A然后更改D中差异的字体