|
楼主 |
发表于 2023-12-7 12:46
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
下面是文心一言写的还不是很完美,请大神修正
Sub CompareText()
Dim ws As Worksheet
Dim rng As Range
Dim i As Long, j As Long
Dim tempTextB As String, tempTextC As String
Dim textDiff As String
Set ws = ThisWorkbook.Sheets("Sheet1") ' 请修改为你的工作表名
Set rng = ws.Range("B3:C22") ' 请确认你的文本范围
' 去除空格并替换特殊符号
For i = 3 To rng.Rows.Count
tempTextB = Trim(rng.Cells(i, 2).Value) ' B列的文本
tempTextC = Trim(rng.Cells(i, 3).Value) ' C列的文本
' 替换特殊符号
tempTextB = Replace(tempTextB, ",", ",")
tempTextB = Replace(tempTextB, ":", ":")
tempTextB = Replace(tempTextB, "!", "!")
rng.Cells(i, 2).Value = tempTextB
tempTextC = Replace(tempTextC, ",", ",")
tempTextC = Replace(tempTextC, ":", ":")
tempTextC = Replace(tempTextC, "!", "!")
rng.Cells(i, 3).Value = tempTextC
Next i
' 比较并标记不同字符
For i = 2 To rng.Rows.Count
tempTextB = ws.Cells(i, 2).Value
tempTextC = ws.Cells(i, 3).Value
' 找出不同的字符并标记为红色
For j = 1 To Len(tempTextB) - 1
If Mid(tempTextB, j, 1) <> Mid(tempTextC, j, 1) Then
If textDiff = "" Then
textDiff = Mid(tempTextB, j, 1) & "<>" & Mid(tempTextC, j, 1)
Else
textDiff = textDiff & "<>" & Mid(tempTextB, j, 1) & "<>" & Mid(tempTextC, j, 1)
End If
End If
Next j
' 把不同字符标记为红色
If Len(textDiff) > 0 Then
ws.Cells(i, 2).Characters(Len(tempTextB) - Len(textDiff), Len(textDiff)).Font.Color = RGB(255, 0, 0)
ws.Cells(i, 3).Characters(Len(tempTextC) - Len(textDiff), Len(textDiff)).Font.Color = RGB(255, 0, 0)
textDiff = "" ' 重置textDiff变量以便下一行的比较
End If
Next i
End Sub
|
|