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

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
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,213,510
Messages
6,114,034
Members
448,543
Latest member
MartinLarkin

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