Using VBA to copy data from a closed workbook to the current, open workbook

ftmthe3rd

New Member
Joined
May 11, 2012
Messages
3
Like the title says, I'd like to copy data from a closed Excel workbook to the current one (which also houses the macro). I'm using Excel 2007.

The current workbook is named Carrier_Change.xlsx, and has one worksheet named "Analysis" where I'd like to copy the data to. It also has the macro that I'm running in it.

The closed file (which I'll refer to as the Report) also has one worksheet, but its location and name can vary. To find that Report, I've got a small function that prompts the user to select the closed report:

Code:
Dim ReportWbk As Workbook 'workbook with report data
Dim Report As String 'name of file with report data
Application.FileDialog(msoFileDialogFilePicker).Show
Report = Application.FileDialog(msoFileDialogFilePicker).SelectedItems(1)
Set ReportWbk = Workbooks.Open(Report)
ReportWbk.Sheets(1).Cells.Select
Selection.Copy
Beyond this, I'm stumped. I feel it must be trivial to do, but I can't figure out how to select the Carrier_Change workbook, paste the data in the "Analysis" worksheet, and close the Report workbook.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
If you have no further use for the Report book, then, if you close it, the right workbook will be active - but try this code:

Code:
Dim ReportWbk As Workbook 'workbook with report data
Dim Report As String 'name of file with report data
Application.FileDialog(msoFileDialogFilePicker).Show
Report = Application.FileDialog(msoFileDialogFilePicker).SelectedItems(1)
Set ReportWbk = Workbooks.Open(Report)
ReportWbk.Sheets(1).Cells.Copy
ReportWbk.Close (False)
ThisWorkbook.Sheets("Analysis").Activate
Cells(1, 1).Select: ActiveSheet.Paste

<table border="0" cellpadding="0" cellspacing="0" width="342"><colgroup><col width="342"></colgroup><tbody><tr height="17"> <td style="height:12.75pt;width:257pt" height="17" width="342">You can't do one thing. XLAdept</td> </tr></tbody></table>
 
Upvote 0
That works great, thank you.

The only issue is that when Excel closes the Report workbook, I get a popup regarding the large amount of data on the clipboard (which I obviously want to keep there). Is there another function I can add before I close that workbook that would automatically tell Excel to save all data to clipboard? If not, no big deal.
 
Upvote 0
Hi FTMthe3rd,

Try:

Code:
Application.DisplayAlerts = False

But when I ran it, it had already pasted so I didn't need it anymore.

<table border="0" cellpadding="0" cellspacing="0" width="342"><colgroup><col width="342"></colgroup><tbody><tr height="17"> <td style="height:12.75pt;width:257pt" height="17" width="342">You can't do one thing. XLAdept</td> </tr></tbody></table>
 
Upvote 0
Greetings all,

I looked up this thread and am having difficulty replicating this procedure in something similar that I'm trying to do. It is slightly modified as I need it to paste only the values from DDE data which is slow to populate. I am able to successfully get through the procedure, but then I get a Runtime error that the 'PasteSpecial method of Range class failed'. Tried a number of different things but I can't pinpoint exactly what I'm doing wrong.

Note: This is a subprocedure and both workbooks are open at the time of the code running as it starts out of the target workbook. The target workbook also always changes in filename when the date changes, which is why I haven't declared any variables for each of the workbooks in this subprocedure.

Code:
Range("A:U").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy


Application.CutCopyMode = True
ActiveWorkbook.Close savechanges:=True


ThisWorkbook.Sheets("RDATA STATIC").Activate
Cells(1, 1).Select
Selection.PasteSpecial Paste:=xlValues
Application.CutCopyMode = False
 
Upvote 0
Try copying closer to the pasting:


Code:
Range("A:U").Select Range(Selection, Selection.End(xlDown)).Copy

ThisWorkbook.Sheets("RDATA STATIC").Cells(1, 1).PasteSpecial Paste:=xlValues 

Application.CutCopyMode = False  
ActiveWorkbook.Close savechanges:=True
 
Upvote 0
This is an old post obviously but I find it really helpful on my current project, how about the other way around? the macro should copy the data from the open file to the close file?

If you have no further use for the Report book, then, if you close it, the right workbook will be active - but try this code:

Code:
Dim ReportWbk As Workbook 'workbook with report data
Dim Report As String 'name of file with report data
Application.FileDialog(msoFileDialogFilePicker).Show
Report = Application.FileDialog(msoFileDialogFilePicker).SelectedItems(1)
Set ReportWbk = Workbooks.Open(Report)
ReportWbk.Sheets(1).Cells.Copy
ReportWbk.Close (False)
ThisWorkbook.Sheets("Analysis").Activate
Cells(1, 1).Select: ActiveSheet.Paste

You can't do one thing. XLAdept

<colgroup><col width="342"></colgroup><tbody>
</tbody>
 
Upvote 0

Forum statistics

Threads
1,215,549
Messages
6,125,473
Members
449,233
Latest member
Deardevil

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