+ Reply to Thread
Results 1 to 4 of 4

need to compare between 2 tables

  1. #1

    need to compare between 2 tables

    Hi,

    I have 2 tables and i need to get the unmatches between the 2 tables
    (fast way because its a long tables ~35000 rows)

    thanks for help.


  2. #2
    Martin Fishlock
    Guest

    RE: need to compare between 2 tables

    Use vlookup on the table like:

    =if (isna(vlookup(A1,sheet2!a1:b35000,1,false),"no match","match")

    if the column a contains the unique key, otherwise adjust as required or add
    a column and make the key.
    --
    HTHs Martin


    "[email protected]" wrote:

    > Hi,
    >
    > I have 2 tables and i need to get the unmatches between the 2 tables
    > (fast way because its a long tables ~35000 rows)
    >
    > thanks for help.
    >
    >


  3. #3
    Rick Hansen
    Guest

    Re: need to compare between 2 tables

    You really did leave many details how yours tables were constructed, and if
    yours tables were on different worksheets. Anyways I took a stabe at it.
    You'll probably have to modify the code for your tables, but I think it'll
    you a ideal where you can start. Enjoy...

    Rick
    ==============================================
    Option Explicit

    Sub tableCk()

    Dim lRow As Long '' range total row count
    Dim iCol As Integer '' range total col count
    Dim lRcnt As Long '' row counter in for loop
    Dim iCcnt As Integer '' col counter in for loop
    Dim table1 As Range, table2 As Range '' range var for table 1 & 2
    Dim vTab1 As Variant, vTab2 As Variant '' variant var range array's

    '' assuming both tables have equal rows & cols

    Set table1 = Range("a2:e6") '' set range for table 1
    Set table2 = Range("a10:e14") '' set range for table 2
    lRow = table1.Rows.Count '' need total row count
    iCol = table1.Columns.Count '' need total col count

    '' copy table 1 & 2 into variant variables, which now become
    '' variant arrays.
    '' It faster to compare data in variant arrays(in memory) than it is
    '' to compare Range to Range

    vTab1 = table1 '' make variant array table1
    vTab2 = table2 '' make variant array table2

    '' now compare table
    For lRcnt = 1 To lRow
    For iCcnt = 1 To iCol
    If vTab1(lRcnt, iCcnt) <> vTab2(lRcnt, iCcnt) Then
    MsgBox ("Table 2 is not equal to Table 2, row=" & CStr(lRcnt) &
    ",col=" & CStr(iCcnt))
    Exit Sub
    End If
    Next iCcnt
    Next lRcnt

    MsgBox ("Both Tables are equal")

    End Sub
    ----------------------------------------------------------------------------


    <[email protected]> wrote in message
    news:[email protected]...
    > Hi,
    >
    > I have 2 tables and i need to get the unmatches between the 2 tables
    > (fast way because its a long tables ~35000 rows)
    >
    > thanks for help.
    >




  4. #4
    Peter Rooney
    Guest

    RE: need to compare between 2 tables

    Yuvalbra,

    I don't know how many columns your tables have, but this code compares two
    single column lists. If your tables are multi-column, perhaps you could paste
    the first column from each table into the workbook. The code contains
    comments as to how you should lay your data out on the worksheet.

    Hope this helps

    Pete

    Sub ListCompare()
    Dim CompSheet As Worksheet

    Dim List1 As Range 'range of cells containing your first list eg B3:B32
    Dim List1Header As Range 'label at the top of list 1 e.g. B2
    Dim List1Item As Range

    Dim List2 As Range 'range of cells containing your second list e.g.D3:D32
    Dim List2Header As Range 'label at the top of list 1 e.g. D2
    Dim List2Item As Range

    Dim List1OnlyHeader As Range 'label above where you want items ONLY in
    first list to appear e.g. F2
    Dim List2OnlyHeader As Range 'label above where you want items ONLY in
    second list to appear e.g. H2
    Dim ListBothHeader As Range 'label above where you want items in BOTH
    lists to appear e.g. J2

    Dim Flag As Boolean

    'In my example, List1 is B3:B32 and List2 is D3:D32, although the code
    works out
    'how long the lists are and allocates the names List1 and List2 to the
    cells containing them.

    'Make sure that there is a blank column to the left of List1Header, and
    blank
    'columns between List1OnlyHeader and List2OnlyHeader, and between
    List2OnlyHeader and ListBothHeader.

    'Finally, make sure there is a blank column to the right of
    ListBothHeader.
    'This ensures that all the "CurrentRegion" referenece work correctly.
    'In my example, List1OnlyHeader is a label in F2, List2Header is a label
    in H2 and
    'ListBothHeader is a label in J2. Columns A, D, E, G, I and K must NOT
    contain any entries.
    'The worksheet is called "Compare Lists"

    Set CompSheet = Worksheets("Compare Lists")

    Set List1Header = CompSheet.Range("List1Header")
    Set List1OnlyHeader = CompSheet.Range("List1OnlyHeader")
    Set List2Header = CompSheet.Range("List2Header")
    Set List2OnlyHeader = CompSheet.Range("List2OnlyHeader")
    Set ListBothHeader = CompSheet.Range("ListBothHeader")

    If List1Header.CurrentRegion.Rows.Count = 1 Then
    MsgBox ("You don't have any entries in List 1!")
    Exit Sub
    End If

    If List2Header.CurrentRegion.Rows.Count = 1 Then
    MsgBox ("You don't have any entries in List 2!")
    Exit Sub
    End If

    List1Header.Offset(1, 0).Resize(List1Header.CurrentRegion.Rows.Count -
    1, 1).Name = "List1"
    List2Header.Offset(1, 0).Resize(List2Header.CurrentRegion.Rows.Count -
    1, 1).Name = "List2"

    Set List1 = CompSheet.Range("List1")
    Set List2 = CompSheet.Range("List2")

    'Clear List1 only entries produced when macro last run
    If List1OnlyHeader.CurrentRegion.Rows.Count > 1 Then
    List1OnlyHeader.Offset(1,
    0).Resize(List1OnlyHeader.CurrentRegion.Rows.Count - 1).ClearContents
    End If
    'Clear List2 only entries produced when macro last run
    If List2OnlyHeader.CurrentRegion.Rows.Count > 1 Then
    List2OnlyHeader.Offset(1,
    0).Resize(List2OnlyHeader.CurrentRegion.Rows.Count - 1).ClearContents
    End If
    'Clear ListBoth entries produced when macro last run
    If ListBothHeader.CurrentRegion.Rows.Count > 1 Then
    ListBothHeader.Offset(1,
    0).Resize(ListBothHeader.CurrentRegion.Rows.Count - 1).ClearContents
    End If

    'Check which items are only in list 1 and not in List 2
    For Each List1Item In List1
    Flag = False
    For Each List2Item In List2
    If List2Item.Value = List1Item.Value Then
    Flag = True
    End If
    Next
    If Flag = False Then
    'MsgBox (List1Item.Value & " is only in List 1!")
    List1OnlyHeader.Offset(List1OnlyHeader.CurrentRegion.Rows.Count,
    0).Value = List1Item.Value
    Else
    'MsgBox (List1Item.Value & " is in both Lists!")
    ListBothHeader.Offset(ListBothHeader.CurrentRegion.Rows.Count,
    0).Value = List1Item.Value
    End If
    Next

    'Check which items are only in list 2 and not in List 1
    For Each List2Item In List2
    Flag = False
    For Each List1Item In List1
    If List1Item.Value = List2Item.Value Then
    Flag = True
    End If
    Next
    If Flag = False Then
    'MsgBox (List2Item.Value & " is only in List 2!")
    List2OnlyHeader.Offset(List2OnlyHeader.CurrentRegion.Rows.Count,
    0).Value = List2Item.Value
    Else 'Included only for completeness - you already worked out which
    items
    'were in both lists in the previous loop!
    'MsgBox (List2Item.Value & " is in both Lists!")
    'ListBothHeader.Offset(ListBothHeader.CurrentRegion.Rows.Count,
    0).Value = List2Item.Value
    End If
    Next

    'Sort List1Only list
    List1OnlyHeader.CurrentRegion.Sort Key1:=Range("List1OnlyHeader"), _
    Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, _
    Orientation:=xlTopToBottom

    'Sort List2Only list
    List2OnlyHeader.CurrentRegion.Sort Key1:=Range("List2OnlyHeader"), _
    Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, _
    Orientation:=xlTopToBottom

    'Sort ListBoth list
    ListBothHeader.CurrentRegion.Sort Key1:=Range("ListBothHeader"), _
    Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, _
    Orientation:=xlTopToBottom

    End Sub


    "[email protected]" wrote:

    > Hi,
    >
    > I have 2 tables and i need to get the unmatches between the 2 tables
    > (fast way because its a long tables ~35000 rows)
    >
    > thanks for help.
    >
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1