VBA to merge two tables

Dojorgen

Board Regular
Joined
Mar 1, 2018
Messages
59
Hello again everyone:confused:

Looking for VBA on merging to tables together, i have a query that pulls in tables one below the other but would like for them all to be one table.

Example below, the columns are all the same but just need the bottom table to merge with the above table

any ideas:confused:

https://www.flickr.com/photos/160076805@N08/45359906532/in/dateposted-public/

I have data text in columns A through G in a table, right below that table is another and i was hoping to get VBA for merging the two tables together They would always be 'Table 1' queries as i would keep deleting the same table if that makes sense
 
Last edited by a moderator:

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Try this macro:
Code:
Public Sub Merge_Tables()

    Dim table As ListObject, firstTable As ListObject
    Dim headerRow As Range, tableRange As Range
    
    'Find first (top) table in active sheet
    
    Set firstTable = Nothing
    For Each table In ActiveSheet.ListObjects
        If firstTable Is Nothing Then Set firstTable = table
        If table.Range.Row < firstTable.Range.Row Then Set firstTable = table
    Next
        
    'Merge other tables in active sheet with first table
    
    For Each table In ActiveSheet.ListObjects
        If table.Range.Row <> firstTable.Range.Row Then
            Set headerRow = table.HeaderRowRange
            Set tableRange = table.Range
            table.Unlist
            headerRow.Delete
            firstTable.Resize Union(firstTable.Range, tableRange)
        End If
    Next
    
    firstTable.Name = "Table1"
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,002
Messages
6,122,652
Members
449,092
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top