excel - 检测文本相同但文本格式不同的单元格之间的差异

标签 excel vba

我想检测两个单元格之间的文本是否有任何不同。
例如,单元格 A1 和 B1 具有相同的文本,但文本的格式不同:

Cell A1: This is my cell.

Cell B1: This is my cell.


以下代码没有标记差异:
'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/

相关文章:

excel - 带高亮显示的动态搜索 - Excel VBA

node.js - 在nodejs中使用multer上传xlsx文件时添加自定义字段

excel - Excel 2010 中的连接函数添加单引号和逗号

excel - 在 Excel 查询编辑器中合并具有不同列数的 CSV 文件文件夹

excel - 替换字符串变量中的多个字符 (VBA)

excel - VBA Excel在HTMLObject中获取文本

vba - 循环访问另一个工作簿中的单元格以复制特定值

尝试将工作表复制到另一个工作簿时 VBA 返回错误 1004

vba - 私有(private)模块中的智能感知

vba - 确定使用ms-access的ODBC故障(错误3146)的真正原因?