【问题标题】:Count the same number of characters between two cells in Excel在 Excel 中计算两个单元格之间相同数量的字符
【发布时间】:2015-06-05 03:16:09
【问题描述】:

我很新,不是第一次使用,但可能以前来过一次。

我有一个巨大的名字列表来检查他们是否有重复。

问题是有时列表中的同一个人的同名拼写不同。

所以,我想检查一个单元格的第一个字母,看看它们是否相同。

例如,这是一个虚构的列表:

 A1 John Doe
 A2 John Dowe
 A3 John Dove

我可以使用 Excel 函数来告诉我上面的单元格中有多少个字符是相同的?

因此,在上面的示例中,公式所在的单元格将显示:

A1 John Doe B1 0 (or "NA" or "REF" because there's no cell to compare to above it)
 A2 John Dowe B2 7 
 A3 John Dove B3 7

因为 A2 和 A3 都与它们上面的单元格共享 7 个字符...我希望这很容易理解。

谢谢!

【问题讨论】:

  • 哇...我想不通,但您可能是对的,我只是不知道如何在 Excel 中实现这些...谢谢...
  • 我用手电筒照了那个洞,如果你看得够远的话,我认为有一些 VB/VBA 示例实现。我不再拥有它了,但是我曾经与之共事的一个人实现了 Levenshtein 距离算法。执行起来很慢,但它并不是非常困难的代码。我没有这样做,因为我现在被重定向到另一个方向。

标签: excel vba


【解决方案1】:

看看this question。它询问的是在不同情况下的名称匹配,但那里列出了很多很好的例子来说明如何做到这一点。

这是一个相当深的兔子洞,具体取决于您想要获得的强度和深度。

【讨论】:

  • 是的,你说得对,我正在寻找一个非常复杂的东西...谢谢!...
【解决方案2】:

你没有给我们足够的信息。

在您的情况下,A1(“John Doe”)包含两个“o”字符。因此,下面的单元格 A2(“John Dowe”)将只有 6 个共同字符。

如果你所说的“相同字符”是指相同位置的相同字符,那么你在 A1 和 A2 之间仍然有问题。

约翰·多e
约翰·多we

“w”和“e”不匹配。只有 6 个字符是相同的。

为我们提供更多信息!

编辑 #1
将此代码复制并粘贴到模块中

Function compareCells(ran As Range) As Variant
Dim numSame As Integer

Dim c As Integer
Dim r As Integer
c = ran.Column  'find selected column
r = ran.Row     'find selected row

Dim a As String
Dim b As String
a = Worksheets("Ark1").Cells(r - 1, c).Value    'string of the cell above
b = Worksheets("Ark1").Cells(r, c).Value        'string of the current cell

Dim aLen As Integer
aLen = Len(a)      'check the length of the a string


Dim i As Integer
For i = 1 To aLen
    If Mid(a, i, 1) = Mid(b, i, 1) Then 'if chars in same pos are equal...
        numSame = numSame + 1
    End If
Next

compareCells = numSame

End Function

编辑#2

Function compareCells(ran As Range) As Variant
Dim numSame As Integer

Dim c As Integer
Dim r As Integer
c = ran.Column  'find selected column
r = ran.Row     'find selected row

Dim a As String
Dim b As String
a = ActiveSheet.Cells(r - 1, c).Value    'string of the cell above
b = ActiveSheet.Cells(r, c).Value        'string of the current cell

Dim aLen As Integer
aLen = Len(a)      'check the length of the a string


Dim i As Integer
For i = 1 To aLen
    If Mid(a, i, 1) = Mid(b, i, 1) Then 'if chars in same pos are equal...
        numSame = numSame + 1
    End If
Next

compareCells = numSame

End Function

使用=compareCells() 并选择您要与上面的单元格进行比较的单元格

【讨论】:

  • 好的,让我试试...我的意思是该函数会比较相同位置的相同字符...但我也将空格字符计为一个字符...所以这就是为什么我认为 John Doe 和 John Dowe 有 7 个相同的字符,它们都以 John_Do 开头,如果您将空格算作一个字符...通过查看字符串的长度并在其旁边设置一列这将计算相同字符的数量,我可以设置一个比率,这将帮助我找到相似的名称以便更仔细地查看......
  • 哇!我印象深刻,但我一定做错了什么。我很想尝试一下,但我不知道为什么它对我不起作用。不过,我是新手,所以让我问你几个愚蠢的问题。我将它插入到 Excel 中的“模块”中,然后我尝试通过在括号内插入 BG116 来使用公式 compareCells(),因为那是我的测试单元格。 BG116 顶部和下方都有单元格,但我得到了#VALUE!错误。 BG116 上方的单元格名称以 Tom_ 开头,BG116 也是如此。我知道我一定是做错了什么...我该怎么办?...再次感谢...
  • 我想我知道问题出在哪里。我给我的工作表起了名字,而你没有。我现在已经更新了代码。请参阅我的编辑#2。错误将消失。
猜你喜欢
  • 2013-04-27
  • 1970-01-01
  • 1970-01-01
  • 2016-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-19
  • 1970-01-01
相关资源
最近更新 更多