vba 如何逐个字母比较 2 个相似的字符串并突出显示差异?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2858125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare 2 similar strings letter by letter and highlight the differences?
提问by PowerUser
We have 2 databases that shouldhave matching tables. I have an (In-Production) report that compares these fields and displays them to the user in an MS-Access form (continuous form style) for correction.
我们有 2 个应该有匹配表的数据库。我有一个(生产中)报告,它比较这些字段并以 MS-Access 表单(连续表单样式)向用户显示它们以进行更正。
This is all well and good except it can be difficult to find the differences. How can I format these fields to bold/italicize/color the differences?
这一切都很好,只是很难找到差异。如何格式化这些字段以将差异加粗/斜体/着色?
"The lazy dog jumped over a brown fox."
"The lazy dog jumped over the brown fox."
“懒狗跳过了一只棕色的狐狸。”
“懒狗跳过了棕狐。”
(It's easier to see the differences between 2 similiar text fields once they are highlighted in some way)
(一旦它们以某种方式突出显示,就更容易看到 2 个相似文本字段之间的差异)
"The lazy dog jumped over a brown fox."
"The lazy dog jumped over the brown fox. "
“懒狗跳过了一只棕色的狐狸。”
“懒狗跳过了棕色的狐狸。 ”
Since we're talking about a form in MS Access, I don't have high hopes. But I know I'm not the first person to have this problem. Suggestions?
由于我们讨论的是 MS Access 中的表单,因此我不抱太大希望。但我知道我不是第一个遇到这个问题的人。建议?
Edit
编辑
I've gone with Remou's solution. It's not my ideal solution, but it is "good enough", especially since I don't have any rich text options. In the query that builds the source table, I used space() to add trailing spaces to make both fields of equal length. Then I added this code to the Click event of both fields (with TextA and TextB reversed for the other field):
我已经使用了 Remou 的解决方案。这不是我理想的解决方案,但它“足够好”,尤其是因为我没有任何富文本选项。在构建源表的查询中,我使用 space() 添加尾随空格以使两个字段的长度相等。然后我将此代码添加到两个字段的 Click 事件中(另一个字段的 TextA 和 TextB 反转):
Dim i As Integer
For i = 1 To Len(Me.TextA.Text)
If Right(Left(Me.TextA.Value, i), 1) <> _
Right(Left(Me.TextB.Value, i), 1) Then
Me.TextA.SelStart = i - 1
Me.TextA.SelLength = Len(Me.TextA.Text)
Exit For
End If
Next i
The result is that when you click on each field, the first "differing letter" to the end of the string is selected. I was able to experiment, code, and text this quickly, so I went with it. But I'll be revisiting this idea sooner or later since this concept would be useful in several projects.
结果是当您单击每个字段时,会选择字符串末尾的第一个“不同字母”。我能够快速地进行实验、编码和文本,所以我选择了它。但我迟早会重新审视这个想法,因为这个概念在几个项目中都很有用。
采纳答案by Fionnuala
You could set selstart and sellength, this will select a part of the textbox. There are some dangers, in that the user may lean on a key and clear the selection.
您可以设置 selstart 和 sellength,这将选择文本框的一部分。存在一些危险,因为用户可能靠在一个键上并清除选择。
回答by Albert D. Kallal
Rich text is supported and built into ms-access for the last two version. So, you can build a screen like:
最后两个版本支持富文本并将其内置到 ms-access 中。因此,您可以构建一个屏幕,例如:
The 3rd column in the above is simply bound to a function that displays the difference between the two columns. The above is a actual screen shot running with the following code for the 3rd column/function.
上面的第三列简单地绑定到一个显示两列之间差异的函数。以上是使用以下代码为第三列/函数运行的实际屏幕截图。
Here the code:
这里的代码:
bolSame = True
i1 = 1: i2 = 1
For i = 1 To Len(c2t)
c1 = Mid(c1t, i1, 1)
c2 = Mid(c2t, i2, 1)
s = c2
If c1 = c2 Then
If bolSame = False Then
bolSame = True
s = "</strong></font>" & s
End If
i1 = i1 + 1: i2 = i2 + 1
Else
If bolSame = True Then
bolSame = False
s = "<font color=red><strong>" & s
End If
i1 = i1 + 1: i2 = i2 + 1
End If
strResult = strResult & s
Next
If bolSame = False Then
strResult = strResult & "</strong></font>"
End If
MyCompare = strResult
I really don't think the problem here is producing a string, I the REAL hard problem is when the strings are different lengths. It is FAR from a trivial coding exercise to display differences in two strings. You can certainly display from where they are different on wards, but highlighting each difference is a difficult coding problem.
我真的不认为这里的问题是产生一个字符串,我真正的难题是字符串的长度不同。从一个简单的编码练习中可以看出两个字符串的差异。您当然可以从病房中不同的地方进行显示,但是突出显示每个差异是一个困难的编码问题。
回答by Fink
You could look into using a rich text box control. That would allow you to change fonts, bold, italicize, ect.
您可以考虑使用富文本框控件。这将允许您更改字体,粗体,斜体等。
There are a few issues. If using Access 2003 or XP, then you should avoid the Rich TextBox Control 6.0 http://support.microsoft.com/kb/838010It has nasty security issues, and Microsoft says it should be avoided at all costs.
有几个问题。如果使用 Access 2003 或 XP,那么你应该避免使用 Rich TextBox Control 6.0 http://support.microsoft.com/kb/838010它有严重的安全问题,微软表示应该不惜一切代价避免它。
If you are using 2003 or XP you can look into this rich text control http://www.lebans.com/richtext.htmIts older but should work with 2003 and XP. I've never used it personally, so I'm not sure how well it works.
如果您使用的是 2003 或 XP,您可以查看此富文本控件http://www.lebans.com/richtext.htm它较旧,但应该适用于 2003 和 XP。我从来没有亲自使用过它,所以我不确定它的效果如何。
回答by David-W-Fenton
Access 2007 introduced a vastly improved rich textbox that understands HTML and if I felt it was important to do something more than @Remou's suggestion of selecting the differing text, I'd delve into that.
Access 2007 引入了一个大大改进的富文本框,可以理解 HTML,如果我觉得做一些比@Remou 选择不同文本的建议更重要的事情,我会深入研究。
I'd be much more concerned about how to write the code that figures out what's different between the two examples. That seems to me like a much, much harder problem.
我会更关心如何编写代码来找出两个示例之间的不同之处。在我看来,这似乎是一个困难得多的问题。