2015-06-29 37 views
0

第一个问题在这里。我正在寻找一种方法来实质比较2个小数据集/表,并在表1的列a中查找附加或不存在于“主”表中的值,并在第三列中包含一些消息。这是在VBA中。使用VBA在单独的工作表中双向比较两个数据表

这可能更容易解释我希望作为输出给出2个示例表。

表1中列Sheet 1中的a和b:

A  B 
a12  horse 

b23  dog 

f54  cat 

表2中列Sheet 2中的a和b:

A  B 
b23  dog 

f54  cat 

i09  tiger 

希望的输出:

A12马警告:该是表2中不存在的附加值

b23狗

F54猫

I09虎警告:此值预期,但您的帮助不存在于表1

感谢,让我知道,如果有更多的细节可以提供,使这个比较容易回答

+0

你觉得我的回答有用吗?如果你这样做,请将其标记为答案。 – HarveyFrench

回答

0

首先要注意两次是同样的问题。

本质上需要扫描表A的行以查看它们是否在表B中。在哪里输出行,其中只有表A具有行输出带有消息的行。

所以,你应该首先使用表1作为表A,然后再使用表2作为表B.在第一个结尾添加第二个输出。

如果不了解更多关于VBA功能的知识,很难提供更多建议。 另外你的例子提供了两列。是否需要对两列进行比较,还是只需要比较一列?我已经假设(最坏的情况)。

您确实在使用UNION集合运算符来添加所有唯一行,以便您可以调整使用的方法here。您必须根据需要描述哪一行是唯一的表格。或者,你可以这样写VBA循环,这将是这样的事情(我认为这会给你你需要的东西)。

将其粘贴到新模块中并运行Main()。您将需要定义表单和范围。

Option Explicit 

Dim s1 As Worksheet 
Dim s2 As Worksheet 
Dim sOutput As Worksheet 
Dim NextOutputRow As Range 


Sub CompareTwoTables(TableA As Range, TableB As Range, NameOfTableB As String, OutputIfRowsMatch As Boolean) 

    Dim TableArow As Long 
    Dim TableBrow As Long 
    Dim TableACell As Range 
    Dim TableBCell As Range 
    Dim FoundMatchingRow As Boolean 
    Dim ColumnDifferencesDetected As Boolean 

    TableA.Parent.Select ' useful for debugging - selects teh sheet 

    For TableArow = 1 To TableA.Rows.Count 

     FoundMatchingRow = False 

     For TableBrow = 1 To TableB.Rows.Count 

      ColumnDifferencesDetected = False 

      Set TableACell = TableA.Cells(TableArow, 1) 
      Set TableBCell = TableB.Cells(TableBrow, 1) 

      TableACell.Select ' useful for debugging 
      Debug.Print TableACell.Address, TableBCell.Address ' useful for debugging 

      If TableACell.Value = TableBCell.Value Then 
       If TableA.Cells(TableArow, 2) = TableB.Cells(TableBrow, 2) Then 
        FoundMatchingRow = True 
       Else 
        ColumnDifferencesDetected = True 
       End If 

      End If 


      If FoundMatchingRow Or ColumnDifferencesDetected Then 
       Exit For ' TableBrow 
      End If 

     Next TableBrow 


     If FoundMatchingRow Then 
      If OutputIfRowsMatch Then 
       NextOutputRow.Cells(1, 1) = TableA.Cells(TableArow, 1) 
       NextOutputRow.Cells(1, 2) = TableA.Cells(TableArow, 2) 

       Set NextOutputRow = NextOutputRow.Offset(1, 0) 

      End If 

     ElseIf ColumnDifferencesDetected Then 

      NextOutputRow.Cells(1, 1) = TableA.Cells(TableArow, 1) 
      NextOutputRow.Cells(1, 2) = TableA.Cells(TableArow, 2) 
      NextOutputRow.Cells(1, 2) = "One only one column was the same" 

      Set NextOutputRow = NextOutputRow.Offset(1, 0) 

     Else 
      NextOutputRow.Cells(1, 1) = TableA.Cells(TableArow, 1) 
      NextOutputRow.Cells(1, 2) = TableA.Cells(TableArow, 2) 
      NextOutputRow.Cells(1, 3) = "This value was expected but not present in " & NameOfTableB 

      Set NextOutputRow = NextOutputRow.Offset(1, 0) 

     End If 



    Next TableArow 

End Sub 

Sub main() 

    Dim Table1 As Range 
    Dim Table2 As Range 

    ' Three sheets must exist 
    Set s1 = Worksheets("Sheet1") 
    Set s2 = Worksheets("Sheet2") 
    Set sOutput = Worksheets("Sheet3") 

    Set Table1 = s1.Range("A2:B10") ' Allows for a title row and two columns 
    Set Table2 = s2.Range("A2:B10") 

    ' Clear any previous output 
    sOutput.Cells.ClearContents 

    Set NextOutputRow = sOutput.Range("2:2") ' Allows for a title row 

    CompareTwoTables TableA:=Table1, TableB:=Table2, NameOfTableB:="Table2", OutputIfRowsMatch:=True 

    CompareTwoTables TableA:=Table2, TableB:=Table1, NameOfTableB:="Table1", OutputIfRowsMatch:=False 

    sOutput.Select 

    MsgBox "Done" 

End Sub