excel中的细胞细胞差异

时间:2017-11-16 17:26:09

标签: excel vba excel-vba excel-formula

我已经看到了几个类似的问题,但我发现的任何事情都没有给我带来好处。

我在两张不同的床单上有两张桌子。表本身的结构相同,但包含不同的内容。表中的每个数据单元都可以有多行信息(例如E5是5行(实际行,未包装)数据)。

我想创建一个第三个表,它在结构上也是相同的(第1行和第A列是相同的)到前两个,但每个数据单元包含一些有意义的语法,相同的两个之间的区别前两个表中的单元格。它可以像前两个单元格内容之间的差异输出一样简单。

有没有办法基本上采用sheet1!E5sheet2!E5的差异并将其存储在sheet3!E5

编辑: 一些澄清。所有数据单元都包含有关MULTIPLE行的信息。有些线条存在于一张纸中但不存在于另一张纸中。例如,sheet1!E5可能包含" string1"," string2"和" string3"在三行上,sheet2!E5包含" string2"," string3"," string7"," string8"在四行。例如sheet3!E5上的输出可以清楚地标出两者之间的差异(如前所述),如果两个单元格是文本文件并且运行了diff(或diff),那么输出将是什么-c)就像这样:

*** 1,3 ****
- string1
  string2
! string3
\ No newline at end of file
--- 1,4 ----
  string2
! string3
! string7
! string8
\ No newline at end of file

甚至只是更简单的东西,它列出了每个单元格中不存在的字符串,如:

<  string1
<> string2
<> string3
>  string7
>  string8

2 个答案:

答案 0 :(得分:1)

使用MSDN documentation处的代码,我制作了以下VBA代码。它不是逐字的,因为VBA不允许短路操作符,我需要PrintDiff返回字符串而不是显示它。

Reflection

如果A1包含:

Function LCSLength(C() As Integer, X() As String, Y() As String, M As Integer, N As Integer) As Integer

    Dim I As Integer
    For I = 0 To M
        C(I, LBound(Y)) = 0
    Next

    Dim J As Integer
    For J = 0 To N
        C(LBound(X), J) = 0
    Next

    For I = 1 To M
        For J = 1 To N
            If X(I) = Y(J) Then
                C(I, J) = C(I - 1, J - 1) + 1
            ElseIf C(I, J - 1) < C(I - 1, J) Then
                C(I, J) = C(I - 1, J)
            Else
                C(I, J) = C(I, J - 1)
            End If
        Next
    Next

    LCSLength = C(M, N)

End Function

Function PrintDiff(C() As Integer, X() As String, Y() As String, I As Integer, J As Integer) As String

    Continue = 1

    If Continue = 1 And I > 0 And J > 0 Then
        If X(I) = Y(J) Then
            PrintDiff = PrintDiff(C, X, Y, I - 1, J - 1) & Chr(10) & "<> " & X(I)
            Continue = 0
        End If
    End If

    If Continue = 1 And J > 0 Then
        If I = 0 Then
            PrintDiff = PrintDiff(C, X, Y, I, J - 1) & Chr(10) & ">   " & Y(J)
            Continue = 0
        ElseIf C(I, J - 1) >= C(I - 1, J) Then
            PrintDiff = PrintDiff(C, X, Y, I, J - 1) & Chr(10) & ">   " & Y(J)
            Continue = 0
        End If
    End If

    If Continue = 1 And I > 0 Then
        If J = 0 Then
            PrintDiff = PrintDiff(C, X, Y, I - 1, J) & Chr(10) & "<   " & X(I)
            Continue = 0
        ElseIf C(I, J - 1) < C(I - 1, J) Then
            PrintDiff = PrintDiff(C, X, Y, I - 1, J) & Chr(10) & "<   " & X(I)
            Continue = 0
        End If
    End If

    If Continue = 1 Then
        PrintDiff = ""
    End If

End Function

Function Diff(A As String, B As String) As String

    Dim X() As String
    X = Split(Chr(10) & A, Chr(10))

    Dim M As Integer
    If (A = "") Then
        M = 0
    Else
        M = UBound(X)
    End If

    Dim Y() As String
    Y = Split(Chr(10) & B, Chr(10))

    Dim N As Integer
    If (B = "") Then
        N = 0
    Else
        N = UBound(Y)
    End If

    Dim C() As Integer
    ReDim C(M, N) As Integer

    Call LCSLength(C, X, Y, M, N)
    Diff = Mid(PrintDiff(C, X, Y, M, N), 2)

End Function

B1包含:

string1
string2
string3

如果C1包含string2 string3 string7 string8 ,那么C1将显示:

=Diff(A1,B1)

根据&#34;代码优化&#34;该页面上的部分可以对代码进行一些优化 - 我还没有在这段代码中完成这些优化。

答案 1 :(得分:0)

是。在Sheet 3中复制您的结构,并确保所有引用都准确无误。例如,在表3的单元格A1中,您只需键入:

= [navigate to sheet1!A1 and click it] - [navigate to sheet2!A1 and click it]
实际上很容易。