你没有给我们足够的信息。
在您的情况下,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() 并选择仅您要与上面的单元格进行比较的单元格