简体   繁体   English

在Excel中的两个单元格之间计算相同数量的字符

[英]Count the same number of characters between two cells in Excel

I'm pretty new, not a first time user, but maybe been here once before. 我是新手,不是第一次来,但也许以前来过这里。

I have a huge list of names to check if they have duplicates. 我有大量的名称列表,以检查它们是否重复。

The problems is that sometimes the same name is spelled differently for the same person in the list. 问题在于,有时列表中的同一人的名字拼写不同。

So, I'd like to check the first letters of a cell to see if they are the same. 因此,我想检查一个单元格的前几个字母是否相同。

For example, here's a made-up list: 例如,这是一个虚构列表:

 A1 John Doe
 A2 John Dowe
 A3 John Dove

Is there an Excel function I can use that will tell me how many characters are the same from the cell above it? 我可以使用一个Excel函数来告诉我上面的单元格中有多少个字符吗?

So, in the example above the cells where the formula would be, would display: 因此,在上面的示例中,公式所在的单元格将显示:

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

Because both A2 and A3 share 7 characters with the cell above them... I hope this is easy to understand. 因为A2和A3都与它们上方的单元格共享7个字符,所以我希望这易于理解。

Thanks! 谢谢!

Take a look at this question . 看一下这个问题 It's asking about name matching under a different situation, but there are plenty of good examples of how to do it listed there. 它询问有关在不同情况下进行名称匹配的问题,但是在那里列出了很多很好的示例。

It's a pretty deep rabbit hole depending on how intense and in-depth you want to get. 这是一个非常深的兔子洞,具体取决于您想要获得的强度和深度。

You are NOT giving us enough info. 您没有给我们足够的信息。

In your case, A1 ("John Doe") contains two "o" characters. 在您的情况下,A1(“ John Doe”)包含两个“ o”字符。 Because of that, the cell below, A2 ("John Dowe") will have only 6 characters in common. 因此,下面的单元格A2(“ John Dowe”)将只有6个字符。

IF what you mean by "same character" is same character in the same position, then you still have got a problem, between A1 and A2. 如果您所说的“相同字符”是在相同位置的相同字符,那么在A1和A2之间仍然存在问题。

John Do e 约翰·做电子商务
John Do w e 约翰待办事项 E

"w" and "e" don't match. “ w”和“ e”不匹配。 Only 6 characters is same. 只有6个字符相同。

Provides us more info! 为我们提供更多信息!

EDIT #1 编辑#1
Copy and paste this code in the Module 将此代码复制并粘贴到模块中

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

EDIT #2 编辑#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

Use =compareCells() and select only the cell you want to compare with the cell above 使用=compareCells()选择要与上面的单元格进行比较的单元格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel VB计算两个单元格之间的周数 - Excel VB count the number of weeks between two cells 使用VBA Excel,我试图计算条目之间一列中的空白单元格的数量 - Using VBA Excel I am trying to count the number of blank cells in a column between entries Excel VBA计算两个单元格之间的差异+如果超出差异则添加字符 - Excel VBA Calculating difference between two cells + adding characters if difference is exceeded Excel:自动复制一个值的次数等于同一行中非零单元格的计数 - Excel: Automate copying a value a number of times equivalent to the Count of non-zero cells in the same row Excel宏可根据带有数据的两个单元格之间的行数来插入或删除行 - Excel Macro to insert or delete rows based on the number of rows between two cells with data 比较具有相同数量单元格的两个行范围 - compare two row ranges with same number of cells 自定义函数来计算Excel 2010中一列中已用单元格的数量 - Custom Function to Count Number of Used Cells in a Column in Excel 2010 如何计算Excel工作表中彩色单元格的数量? - How would I count the number of coloured cells in an Excel worksheet ? 如何计算Excel工作表中列中已填充单元格的数量 - how to count of the number of filled cells in a column in an excel sheet Excel:计算非连续范围内的相同数字值 - Excel: Count same number values in noncontiguous range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM