Copy Excel Table to a new workbook by removing all connections

alishern

New Member
Joined
Dec 9, 2017
Messages
29
Hello,

I have an excel workbook with an excel Table with bunch of queries. I want to be able to have a user to create his own copy of the table only, i.e. without all the connections to the queries.

Here is the coordinate of the excel table: Worksheets("ESTIMATES_ROLLUP").Range("Table_ESTIMATES_ROLLUP[#All]")

Below is a code snippet i used from an old existing thread. Modified as much as i could to meet my requirements. Above is the excel table's details. A user needs to be able to create a copy of that table.

Here is what i want in a nutshell:

1. To be able to create a new workbook with a copy of the Excel Table "Table_ESTIMATES_ROLLUP" only (below is the location details) and without any connections to the queries:
Worksheets("ESTIMATES_ROLLUP").Range("Table_ESTIMATES_ROLLUP[#All]")

2. I want to have a button that activates the code

As you can see, the code exports a copy to the existing file path and defines the file name with a date stamp, I want to keep that too. When I activate this code, it gives me error saying there is a bug.

Thank you in advance.

***********************************Beginning of Code*******************************************************************************************************************************
VBA Code:
Public Sub ExportNewUpdate()

Dim newWb As Workbook
Dim wbConn As WorkbookConnection
Dim wbQuery As WorkbookQuery
Dim links As Variant, i As Long
Dim xStrDate As String
Dim btn As Shape

Application.ScreenUpdating = False

xStrDate = Format(Now, "yyyymmdd_HH-mm")

ThisWorkbook.Worksheets(Array("ESTIMATES_ROLLUP")).Copy
Set newWb = ActiveWorkbook

'Delete workbook connections

For Each wbConn In newWb.Connections
wbConn.Delete
Next


'Delete workbook queries

For Each wbQuery In newWb.Queries
wbQuery.Delete
Next


'Delete workbook button

For Each btn In newWb.Shapes
btn.Delete
Next

links = newWb.LinkSources(xlExcelLinks)
If links <> Empty Then
For i = 1 To UBound(links)
newWb.BreakLink links(i), xlLinkTypeExcelLinks
Next
End If


Application.DisplayAlerts = False 'suppress warning message displayed if new workbook already exists
newWb.SaveAs ThisWorkbook.Path & "\PE Rollup Report " & " " & xStrDate & ".xlsx"
Application.DisplayAlerts = True

newWb.Close False

MsgBox ("Your new report has been exported to the same location with filename: " & "PE Rollup Report " & " " & xStrDate & ".xlsx")

Application.ScreenUpdating = True


End Sub

***********************************End of Code*******************************************************************************************************************************
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Shapes is the property of the Worksheet object. Therefore, try the following instead...

VBA Code:
For Each btn In newWb.Worksheets(1).Shapes

Hope this helps!
 
Upvote 0
It is throwing this error
1695231966515.png
 
Upvote 0
Use the IsEmpty function instead...

VBA Code:
If Not IsEmpty(Links) Then

Hope this helps!
 
Upvote 0
It did it, thank you!

The code creates a new wb by copying the tab into a new wb instead of exporting only the table, as per the requirement, but I am good with this result. Thanks again.
 
Upvote 0
It did it, thank you!
You're very welcome!

The code creates a new wb by copying the tab into a new wb instead of exporting only the table, as per the requirement, but I am good with this result. Thanks again.
To copy only the table to a new workbook, try replacing...

VBA Code:
ThisWorkbook.Worksheets(Array("ESTIMATES_ROLLUP")).Copy
Set newWb = ActiveWorkbook

with

VBA Code:
Set newWb = Workbooks.Add(xlWBATWorksheet)

ThisWorkbook.Worksheets("ESTIMATES_ROLLUP").Range("Table_ESTIMATES_ROLLUP[#All]").Copy _
    Destination:=newWb.Worksheets(1).Range("A1")

Hope this helps!
 
Upvote 1
Actually, here's an alternative that simply copies the table, without any related queries, connections, and links....

VBA Code:
    ThisWorkbook.Worksheets("ESTIMATES_ROLLUP").Range("Table_ESTIMATES_ROLLUP[#All]").Copy
   
    Set newWb = Workbooks.Add(xlWBATWorksheet)
   
    With newWb.Worksheets(1).Range("A1")
        .PasteSpecial xlPasteValues
        .PasteSpecial xlPasteFormats
    End With

Hope this helps!
 
Upvote 0
You're very welcome!


To copy only the table to a new workbook, try replacing...

VBA Code:
ThisWorkbook.Worksheets(Array("ESTIMATES_ROLLUP")).Copy
Set newWb = ActiveWorkbook

with

VBA Code:
Set newWb = Workbooks.Add(xlWBATWorksheet)

ThisWorkbook.Worksheets("ESTIMATES_ROLLUP").Range("Table_ESTIMATES_ROLLUP[#All]").Copy _
    Destination:=newWb.Worksheets(1).Range("A1")

Hope this helps!
Domenic, thanks for the great solutions. Both solutions work flawlessly, but I had to go with this one as it takes care of saving the file as a new workbook.

VBA Code:
Set newWb = Workbooks.Add(xlWBATWorksheet)

ThisWorkbook.Worksheets("ESTIMATES_ROLLUP").Range("Table_ESTIMATES_ROLLUP[#All]").Copy _
    Destination:=newWb.Worksheets(1).Range("A1")
 
Upvote 0
Actually, I meant that it could be adopted as follows...

VBA Code:
Public Sub ExportNewUpdate()

    Dim newWb As Workbook
    Dim xStrDate As String
    
    Application.ScreenUpdating = False
    
    xStrDate = Format(Now, "yyyymmdd_HH-mm")
    
     ThisWorkbook.Worksheets("ESTIMATES_ROLLUP").Range("Table_ESTIMATES_ROLLUP[#All]").Copy
    
     Set newWb = Workbooks.Add(xlWBATWorksheet)
    
     With newWb.Worksheets(1).Range("A1")
         .PasteSpecial xlPasteValues
         .PasteSpecial xlPasteFormats
     End With
    
    Application.DisplayAlerts = False 'suppress warning message displayed if new workbook already exists
    newWb.SaveAs ThisWorkbook.Path & "\PE Rollup Report " & " " & xStrDate & ".xlsx", xlOpenXMLWorkbook
    Application.DisplayAlerts = True
    
    newWb.Close False
    
    MsgBox ("Your new report has been exported to the same location with filename: " & "PE Rollup Report " & " " & xStrDate & ".xlsx")
    
    Application.ScreenUpdating = True

End Sub

Hope this helps!
 
Upvote 1
Solution
Actually, I meant that it could be adopted as follows...

VBA Code:
Public Sub ExportNewUpdate()

    Dim newWb As Workbook
    Dim xStrDate As String
   
    Application.ScreenUpdating = False
   
    xStrDate = Format(Now, "yyyymmdd_HH-mm")
   
     ThisWorkbook.Worksheets("ESTIMATES_ROLLUP").Range("Table_ESTIMATES_ROLLUP[#All]").Copy
   
     Set newWb = Workbooks.Add(xlWBATWorksheet)
   
     With newWb.Worksheets(1).Range("A1")
         .PasteSpecial xlPasteValues
         .PasteSpecial xlPasteFormats
     End With
   
    Application.DisplayAlerts = False 'suppress warning message displayed if new workbook already exists
    newWb.SaveAs ThisWorkbook.Path & "\PE Rollup Report " & " " & xStrDate & ".xlsx", xlOpenXMLWorkbook
    Application.DisplayAlerts = True
   
    newWb.Close False
   
    MsgBox ("Your new report has been exported to the same location with filename: " & "PE Rollup Report " & " " & xStrDate & ".xlsx")
   
    Application.ScreenUpdating = True

End Sub

Hope this helps!
Yes, it worked. Thank you again!
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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