如何计算excel中两个文本值之间的差异?

时间:2015-07-12 18:25:14

标签: excel excel-formula

两个如何计算excel中两个值之间的差异,是否有一个公式来做

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-XXXXXX-1', 'auto');
  ga('send', 'pageview');

</script>

感谢。

2 个答案:

答案 0 :(得分:2)

也许您正在寻找的是Levenshtein距离 - 两个单词之间的Levenshtein距离是将一个单词更改为另一个单词所需的最小单字符编辑数(即插入,删除或替换)< / em>的

以下UDF可以计算出来,但我没有对它进行过广泛的测试。我发现它的URL现在已经不存在了。它确实为您提供的数字字符串提供了答案。

Option Explicit

'********************************
'*** Compute Levenshtein Distance
'********************************

Public Function LD(ByVal s As String, ByVal t As String) As Long
Dim d() As Long ' matrix
Dim m As Long ' length of t
Dim n As Long ' length of s
Dim i As Long ' iterates through s
Dim j As Long ' iterates through t
Dim s_i As String ' ith character of s
Dim t_j As String ' jth character of t
Dim cost As Long ' cost

  ' Step 1
  n = Len(s)
  m = Len(t)
  If n = 0 Then
    LD = m
    Exit Function
  End If
  If m = 0 Then
    LD = n
    Exit Function
  End If
  ReDim d(0 To n, 0 To m) As Long

  ' Step 2
  For i = 0 To n
    d(i, 0) = i
  Next i

  For j = 0 To m
    d(0, j) = j
  Next j

  ' Step 3
  For i = 1 To n
    s_i = Mid$(s, i, 1)

    ' Step 4
    For j = 1 To m
      t_j = Mid$(t, j, 1)

      ' Step 5
      If s_i = t_j Then
        cost = 0
      Else
        cost = 1
      End If

      ' Step 6
      d(i, j) = Minimum(d(i - 1, j) + 1, d(i, j - 1) + 1, d(i - 1, j - 1) + cost)

    Next j
  Next i

  ' Step 7
  LD = d(n, m)
  Erase d
End Function

'*******************************
'*** Get minimum of three values
'*******************************

Private Function Minimum(ByVal a As Long, _
                         ByVal b As Long, _
                         ByVal c As Long) As Long
Dim mi As Long

  mi = a
  If b < mi Then
    mi = b
  End If
  If c < mi Then
    mi = c
  End If

  Minimum = mi

End Function

答案 1 :(得分:1)

以下公式是您正在寻找的:

=SUBSTITUTE(B1,A1,"")

参考:SUBSTITUTE