我想检测两个单元格之间的文本是否有任何不同。
例如,单元格 A1 和 B1 具有相同的文本,但文本的格式不同:
Cell A1: This is my cell.
Cell B1: This
is mycell.
以下代码没有标记差异:
'if the text in the cells is different in any way, report a difference
If (ActiveSheet.Cells(1, "A") <> ActiveSheet.Cells(1, "B")) Then
ActiveSheet.Cells(1, "C").Value = DIFFERENT
End If
最佳答案
例如:
Sub Tester()
Debug.Print SameText(Range("B4"), Range("C4"))
End Sub
'call from VBA or as UDF
Function SameText(rng1 As Range, rng2 As Range) As Boolean
Dim rv As Boolean, c1, c2, x As Long, arr, v
If rng1.Value = rng2.Value Then
rv = True
arr = Array("Underline", "Fontstyle", "Color") '<< for example
For x = 1 To Len(rng1.Value)
Set c1 = rng1.Characters(x, 1).Font
Set c2 = rng2.Characters(x, 1).Font
For Each v In arr
If CallByName(c1, v, VbGet) <> CallByName(c2, v, VbGet) Then
Debug.Print "Mismatch on " & v & " at position " & x, _
rng1.Address, rng2.Address
rv = False
Exit Function
End If
Next
Next x
Else
rv = False
End If
SameText = rv
End Function
关于excel - 检测文本相同但文本格式不同的单元格之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51314637/