Copy multiple tables keeping filters

JorgenKjer

Board Regular
Joined
Aug 1, 2016
Messages
65
Office Version
  1. 2013
Platform
  1. Windows
Hi<o:p></o:p>
Can anyone help me with an Excel VBA code, which can copy Tables from multiple worksheets to a master sheet?<o:p></o:p>
The Tables Can have different number of columns and rows but are all starting I cell A9 on each worksheet<o:p></o:p>
Before copying,the tables to the master sheet all data on the master sheet from A6 and down and over should be deleted. <o:p></o:p>
Before copyingthe tables all filters should be cleared / reset.<o:p></o:p>
When copyingthe table to the master sheet the formatting of the table should be kept including the filter headings for each table so the filter function still works.<o:p></o:p>
The first copied table is past to cell A6 in the master sheet and ends at for example in cellA30 the next copy should be past at cell A31.<o:p></o:p>
Over time there might be added more worksheets with tables to the workbook. This new worksheet should automatically be included when running the program.<o:p></o:p>
I hope this all makes sense and someone can help me<o:p></o:p>
With best regards<o:p></o:p>
Jorgen<o:p></o:p>
 
Last edited:

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi,

Are these really Tables or are they ranges of data? If they are Tables, then the Table names would be a big help.
 
Upvote 0
Hi igold<o:p></o:p>
Thank youfor showing interest in my problem<o:p></o:p>
The names Tablenames are a mix of English and Danish but can bet change I necessary<o:p></o:p>
BaseFrameAssy<o:p></o:p>
BearingHouse<o:p></o:p>
CouplingHouse<o:p></o:p>
Gearbox<o:p></o:p>
Generator<o:p></o:p>
MainShaft<o:p></o:p>
NacelleForlængerBjælke<o:p></o:p>
NacelleLukkeBjælke<o:p></o:p>
NacelleTransportBjælke<o:p></o:p>
NacelleÅbenBjælke<o:p></o:p>
PCM_TransportBjælke<o:p></o:p>
TippingProtection<o:p></o:p>
TrolleyMSI_V164<o:p></o:p>
I hope thisis what you meant by table names<o:p></o:p>
As mentionedthere will over time be more worksheets each containing a table<o:p></o:p>
Sincerely<o:p></o:p>
Jorgen<o:p></o:p>
 
Upvote 0
Hi Jorgen,

Sorry for the delay.

See if this gets you close to what you are looking for. Given that you are going to be adding sheets and tables in the future, I thought the best way to handle that would be if you added another sheet to your workbook named "Lists". On the "Lists" worksheet, starting in Cell A2 (I left A1 for a header) you will list the names of the tables down Column A in the order that you want them copied to the "Master". Each time you add a new table you must add the name of the new table to the "Lists" worksheet. If you prefer, you do not have add another sheet, but the list of the table names must appear somewhere in your workbook, just be sure to change the reference in the code. Also, make sure that you have a worksheet named "Master".

I hope that makes sense.

Code:
Sub TableCopy()
    
    Dim wsM As Worksheet: Set wsM = Worksheets("Master")
    Dim wsL As Worksheet: Set wsL = Worksheets("Lists")
    Dim ws As Worksheet
    Dim tbl As ListObject
    Dim LstlRow As Long, i As Long, tblRowCt As Long
    Dim tColl As Variant
    
    Application.ScreenUpdating = False
    
    'Assign the list of table names to the tColl array
    wsL.Activate
    tColl = wsL.Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)
    
    'Clear the Master sheet
    wsM.Activate
    wsM.Range(Cells(6, 1), Cells(Rows.Count, Columns.Count)).Clear
    tblRowCt = 6
    
    'Loop through the sheets and match the table names and copy to Master
    For i = LBound(tColl) To UBound(tColl)
        For Each ws In ActiveWorkbook.Worksheets
            If ws.ListObjects.Count > 0 Then
                If ws.ListObjects(1).Name = tColl(i, 1) Then
                    Set tbl = ws.ListObjects(tColl(i, 1))
                    tbl.Range.Copy
                    wsM.Range("A" & tblRowCt).PasteSpecial (xlPasteAll)
                    tblRowCt = tblRowCt + tbl.ListRows.Count + 1
                    Application.CutCopyMode = False
                End If
            End If
        Next
    Next
    
    Range("A1").Select
    Application.ScreenUpdating = True


End Sub

I hope this helps.
 
Upvote 0

Forum statistics

Threads
1,216,499
Messages
6,131,010
Members
449,613
Latest member
MedDash99

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