2015-03-31 61 views
0

我很新,不是第一次使用的用戶,但可能在這裏曾經有一次。計算Excel中兩個單元格之間的字符數相同

我有一個龐大的名單,以檢查他們是否有重複。

問題是,有時同名的拼寫不同,列表中的同一個人。

所以,我想檢查單元格的第一個字母,看它們是否相同。

例如,這裏是一個虛構的名單:

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個字符與它們上面的小區...我希望這是很容易理解。

謝謝!

+0

可能重複的[匹配名稱的好算法?](http://stackoverflow.com/questions/7946192/good-algorithm-for-matching-names) – FreeMan 2015-03-31 18:03:48

+0

哇...我在我的頭上,但你可能是正確的,我只是不知道如何在Excel中實現任何...謝謝... – manny 2015-04-01 19:30:38

+0

我已經把閃光燈放在那個洞,我認爲有一些VB/VBA示例實現,如果你看遠遠不夠。我已經沒有了,但是我曾經和一個人一起工作的人實現了Levenshtein距離算法。執行起來很慢,但代碼並不是非常困難。我沒有做到這一點,因爲我現在已經被另一個方向重定向了。 – FreeMan 2015-04-01 19:34:22

回答

0

看看this question。它是在不同的情況下詢問名稱匹配的問題,但有很多很好的例子說明如何在那裏列出。

這是一個相當深的兔子洞,取決於你想得到多深和深度。

+0

是啊,你是對的,它看起來像一個非常複雜的東西,我正在尋找...謝謝!... – manny 2015-04-01 19:41:49

0

您沒有給我們足夠的信息。

就你而言,A1(「John Doe」)包含兩個「o」字符。因此,下面的單元格A2(「John Dowe」)將只有6個字符的共同點。

IF你所說的「同一個字符」是相同位置的同一個字符,那麼你在A1和A2之間仍然存在問題。

約翰待辦事項Ë
約翰待辦事項W¯¯Ë

「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()並選擇只有你想要的細胞與

0以上的單元格進行比較
+0

好吧,讓我試試...我的意思是,該功能將比較相同的字符相同的位置......但我也將空間角色計爲一個角色......所以,這就是爲什麼約翰·多伊和約翰·多伊算作7個角色一樣,他們都以John_Do開始,如果你數了空間作爲一個字符......通過查看一個字符串的長度並在它旁邊設置一個列來計算相同字符的數量,我可以設置一個比例,這將幫助我找到類似的名稱來查看更密切... – manny 2015-04-01 19:41:05

+0

現在,請參閱我的編輯。經過測試和工作。 – FancyGamer 2015-04-02 04:37:21

+0

哇!我印象深刻,但我一定在做錯事。我急於嘗試這一點,但我無法弄清楚爲什麼它不適合我。不過,我是新來的,所以讓我問你幾個愚蠢的問題。我在Excel中插入了一個'模塊',然後我試圖通過在括號內插入BG116來使用公式compareCells(),因爲那是我的測試單元。 BG116上方和下方都有細胞,但我得到了#VALUE!錯誤。 BG116上方的單元格的名稱以Tom_開頭,BG116也是如此。我知道我一定在做錯事...我該怎麼做?...再次感謝... – manny 2015-04-03 02:17:09

相關問題