Copy specific table columns from one workbook to another

ddualba

New Member
Joined
Dec 15, 2014
Messages
18
Greetings,

I am looking to copy a few columns from a table in another workbook without specifically referring to the sheet name if possible. This is the code I have that works

Code:
Sub Export_SomeData ()


Dim myBook As Workbook, newBook As Workbook


Set myBook = ThisWorkbook
Set newBook = Workbooks.Add




With newBook


    myBook.Sheets("Sheet1").Range("MyDataTable[[#Data], [tColumn_3]]").Copy
        Range("A2").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False




    myBook.Sheets("Sheet1").Range("MyDataTable[[#Data], [tColumn_5]]").Copy
        Range("B2").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False


End With


End sub


I tried using the following code I found in a search to set a range and then copy it but I received the following error on that line when I do.
Error 1004: Application-defined or object-defined error

Code:
set rng = myBook.Names("MyDataTable[[#Data], [tColumn_3]]").RefersToRange

Any ideas on how I could refer/copy a table column in another workbook without referring to the sheet would be appreciated as I want to prevent the code from breaking if the table gets moved or sheet gets renamed.

Thanks
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,892
Hi

Try:


Code:
Sub Export_SomeData()


Dim myBook As Workbook, newBook As Workbook
Dim lstObjFrom As ListObject


Set myBook = ThisWorkbook
Set lstObjFrom = Application.Range("MyDataTable").ListObject
Set newBook = Workbooks.Add

lstObjFrom.ListColumns("tColumn_3").DataBodyRange.Copy
    Range("A2").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False


lstObjFrom.ListColumns("tColumn_5").DataBodyRange.Copy
    Range("B2").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False


End Sub
 
Upvote 0

Forum statistics

Threads
1,191,184
Messages
5,985,175
Members
439,945
Latest member
ospe

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
Top