2015-06-29 52 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