+ Reply to Thread
Results 1 to 4 of 4

Merging two tables

Hybrid View

  1. #1
    Registered User
    Join Date
    04-16-2008
    Posts
    4

    Merging two tables

    I would like to combine two tables. They have one common column(we could maybe assume common column is the first one from the left in the table), and same amount of rows. This is relatively fast to do manually, but doing this hundreds of times would make some macro handy.

    I have attached two excel files that includes examples. However the row and column number might change. I understand some VBA but can't write much code from empty table.

    I have thought two possibility
    1. Creating a macro that sorts tables by the common column and attach the second table without first column to the first table.

    or 2.Using vlookup or lookup functions.

    Any ideas for the solution? If you can provide any help for the code I would be delighted!

    I also found a few commercial programs, but they are not so handy that I would pay 30 dollars (Are there any free?).

    Thank you in advance for any tips!

    btw. I am using -excel 07
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    03-25-2008
    MS-Off Ver
    Excel, Outlook, Word 2007/2003
    Posts
    245
    This one is free.
    Sub attach_2nd_table()
    Dim wsstart As Worksheet
    Dim wstojoin As Worksheet
    Dim mycell As Range
    Dim myfound As Range
    Set wsstart = Worksheets(1)
    Set wstojoin = Worksheets(2)
    For Each mycell In wsstart.Range("A2:A" & _
        wsstart.Range("A" & Rows.Count).End(xlUp).Row)
        With wstojoin.Range("A2:A" & wstojoin.Range("A" & _
                Rows.Count).End(xlUp).Row)
            Set myfound = .Find(mycell, LookIn:=xlValues, LookAt:=xlWhole)
            If Not myfound Is Nothing Then
                myfound.Offset(, 1).Resize(1, 2).Copy mycell.Offset(, 3)
            End If
        End With
    Next mycell
    MsgBox "Finished !", vbInformation
    End Sub
    Charlize

  3. #3
    Registered User
    Join Date
    04-16-2008
    Posts
    4
    Thank you really much for your help Charlize!

    However, I messed up with my post, my intention was to post another table(as now as an attachment) that has more columns. Instead, I posted the same table twice. The code worked great, but is there any way to make it so that it does this work no matter how many columns there are in tables?

    Thanks again..
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    03-25-2008
    MS-Off Ver
    Excel, Outlook, Word 2007/2003
    Posts
    245
    I think this will do the trick.
    Sub attach_2nd_table()
    Dim wsstart As Worksheet, wstojoin As Worksheet
    Dim mycell As Range, myfound As Range
    Dim wsstartcol As Long, wstojoincol As Long
    Set wsstart = Worksheets(1)
    Set wstojoin = Worksheets(2)
    wsstartcol = wsstart.UsedRange.Columns.Count
    wstojoincol = wstojoin.UsedRange.Columns.Count
    For Each mycell In wsstart.Range("A2:A" & _
        wsstart.Range("A" & Rows.Count).End(xlUp).Row)
        With wstojoin.Range("A2:A" & wstojoin.Range("A" & _
                Rows.Count).End(xlUp).Row)
            Set myfound = .Find(mycell, LookIn:=xlValues, LookAt:=xlWhole)
            If Not myfound Is Nothing Then
                myfound.Offset(, 1).Resize(1, wstojoincol - 1).Copy _
                mycell.Offset(, wsstartcol)
            End If
        End With
    Next mycell
    MsgBox "Finished !", vbInformation
    End Sub
    Charlize

+ 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