Consulting

Results 1 to 10 of 10

Thread: Building third large table which consists of matched rows of two smaller tables

  1. #1

    Building third large table which consists of matched rows of two smaller tables

    Hi all,


    I've got a table in Sheet1 with data of for example 100 columns X 10000 rows.
    I've got a table in Sheet2 with data of for example 60 columns X 50000 rows.

    InbBoth tables I have same strings like: PAB10CL401 (lets say column A in Sheet1 and column B in Sheet2...however those strings are in different orders (rows) in both tables.

    I've got one row with PAB10CL401 and in other cell of this row is "+" value and I've got second row with the same string PAB10CL401 and in the same row other cell has "-" value. All in one table. The same thing is in second table. So every string is two times in this one column A or B).

    I want to match both tables so that the strings (here: PAB10CL401) and values "+" and "-" will be the same so it will end up in thrid larger table which is consisting of those 2 sorted tables....

    What sholud algorithm look a like ?

    I believe the 1st criteria should be the string and second the values of +/- from other cell in the row.

    From the input data it is clearly visible that not all rows will be matched (2nd table is bigger) but I want to only match what is possible.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Post a workbook with the two tables and a sample of required third table.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    an example attached.

    Sheet3 is the bigger (wider) table of tables Sheet1 + Sheet2.
    Attached Files Attached Files

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    What about an example of where one sheet has a row not on the other.

    Where one sheet has multiples of a code, will the other always have none or the same number?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5

  6. #6
    I cannot attach file and dont know why...

  7. #7
    I must do 5 post to add a link so this is fifth ;-)

  8. #8
    [vba]http://speedy.sh/xuRwF/Clear-DATA-sample2.xlsx[/vba]

  9. #9
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    In the attached file is this macro. It's not especially efficient. It works by first copying sheet2 then adding to the copied sheet the matching rows from sheet1.
    If there are no matching rows, nothing is added to the new sheet. Where/If there is more than one match on sheet1 (I've added a couple of duplicates on sheet1 for testing), rows are inserted in the new sheet.
    [VBA]Sub blah()
    Set Sht1 = Sheets("Sheet1")
    Sht1LR = Sht1.Cells(Sht1.Rows.Count, "A").End(xlUp).Row
    Sheets("Sheet2").Copy After:=Sheets(Sheets.Count)
    Set ResultSheet = ActiveSheet
    ResultSheet.Name = "Results"
    With ResultSheet
    For S2rw = .Cells(.Rows.Count, "A").End(xlUp).Row To 1 Step -1
    OneAlreadyFound = False
    myCode = .Cells(S2rw, "A").Value
    mySign = .Cells(S2rw, "B").Value
    For S1rw = 1 To Sht1LR
    If Sht1.Cells(S1rw, "C").Value = myCode And Sht1.Cells(S1rw, "E").Value = mySign Then
    If OneAlreadyFound Then
    'insert row:
    .Rows(S2rw + 1).Insert
    Sht1.Cells(S1rw, "A").Resize(, 45).Copy .Cells(S2rw + 1, "CF")
    Else
    Sht1.Cells(S1rw, "A").Resize(, 45).Copy .Cells(S2rw, "CF")
    OneAlreadyFound = True
    End If
    End If
    Next S1rw
    Next S2rw
    End With
    End Sub
    [/VBA]
    Attached Files Attached Files
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  10. #10
    Thanks... I will test it.

Posting Permissions

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