copying a table between workbooks with VBA

miconian

Well-known Member
Joined
Aug 18, 2004
Messages
769
I want to copy a table from one workbook to another. The copied table will then become the source data for a pivot table, so I would like it to stay a table, instead of just being pasted data.

When I try it with the macro recorder I get this:

Code:
Sub copyTable()


    Application.Goto Reference:="EstimatesData"
    Selection.Copy
    Workbooks.Add
    ActiveSheet.Paste

End Sub

The problem is that this does not paste the headers of the table; just the data.

I can copy the table with the headers too like this:

Code:
Range("EstimatesData[[#Headers],[FirstColumn]]").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Workbooks.Add
    ActiveSheet.Paste

...but that strikes me as rather clumsy.

Surely there is a way to tell VBA to just copy the entire table, including the headers?

Or perhaps there is some higher-level functionality for just copying a table between two workbooks, and I'm just missing the big picture?

Thanks!
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Try...

Code:
Range("EstimatesData[#All]").Select
    Selection.Copy
    Workbooks.Add
    ActiveSheet.Paste

Cheers,
Gino
 
Upvote 0
Hm. That works. Thanks.

So how do I then rename the table?

I was trying to do something like this:

Code:
 ActiveWorkbook.Names.Add Name:="EstimatesData",Activeworksheet.Selection

But apparently those are the wrong arguments for Add Name. I'm having trouble figuring out the proper syntax.

I guess we could also assume that since this is a brand new workbook, the table must be Table1, and then rename it, but I'm not sure how to do that either.

thanks...
 
Upvote 0
I think this will work.

Code:
ActiveWorkbook.Names.Add Name:="EstimatesData"
Activesheet.Paste
ActiveSheet.ListObjects.Add(xlSrcRange, Range("A1").CurrentRegion, , xlYes).Name = "MyNew_tbl"

Cheers,
Gino
 
Upvote 0
I'm having trouble putting this all together. What I'm trying is this, but it's not working:

Code:
Sub moveData()

 
Range("EstimatesData[#All]").Select
    Selection.Copy
    Workbooks.Add

ActiveWorkbook.Names.Add Name:="EstimatesData"
ActiveSheet.Paste
ActiveSheet.ListObjects.Add(xlSrcRange, Range("A1").CurrentRegion, , xlYes).Name = "MyNew_tbl"

End Sub

I get an object error on the first line unless the EstimatesData table is already selected. Then, if it is, I get an error on the ActiveWorkbook line.

Sorry for being obtuse...
 
Upvote 0
This works for me. I created a file called Book1.xlsm, set up a table named EstimatesData and added this code to a module.


Code:
Sub Macro2()
'
' Macro2 Macro
'
    Range("EstimatesData[#All]").Select
    Selection.Copy
    Workbooks.Add
    ActiveSheet.Paste
ActiveWorkbook.SaveAs Filename:="C:\your dir\EstimatedData.xlsx"
End Sub

Executed the procedure from within Book1.xlsm (making sure the original table was not selected) and it created EstimatedData.xlsx with a table named "EstimatesData" and with the headers.

Not sure why it's not working for you. I'm on Excel 2010.

Gino
 
Upvote 0
Doesn't work for me. I'm in 2007. Anyway, that's fine, I think I am going to take a different approach and just copy the worksheet from one workbook to the other.
 
Upvote 0
when you do that, does it preserve the table as a table?
Doesn't work for me. I'm in 2007. Anyway, that's fine, I think I am going to take a different approach and just copy the worksheet from one workbook to the other.
 
Upvote 0
I am trying similar but cannot get the following to work (falling down at the command below the comment 'copy reference table. I have also asked additional questions with comments ****. Thanks.

Private Sub Macro_Update_Click()

'open reference file to copy table from
Range("A11").Select
Workbooks.Open Filename:= _
"P:\........file path..........\MASTER Register.xlsm"

Windows("MASTER Register.xlsm").Activate

'copy reference table
Range("Table_Register").Select
Selection.Copy

'Navigate back to source file
Windows("New Master.xlsm").Activate

'****How do I make this save original file name as macro needs to work from different files???

'first row of table
Range("A11").Select
ActiveSheet.Paste
' Range("Table_Register[#All]").Paste

Selection.Replace What:="'MASTER Register.xlsm'!Table_Register", _
Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:= _
False, SearchFormat:=False, ReplaceFormat:=False
Range("A11").Select


Windows("MASTER Register.xlsm").Activate
ActiveWindow.Close

'****Also wold like to b able to close the file without the save file prompt or retain clipboard info*****

' Sets cell with updated date (today)
Range("B8") = Date

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,884
Messages
6,181,568
Members
453,053
Latest member
Kiranm13

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