Record an Excel Macro to Copy Data Between Workbooks
Question details
Create an Excel macro that copies data or sparkline images from another workbook into the current workbook or a new one.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- The user wants to automate a copy operation involving a source workbook and a separate destination workbook.
- Observed behavior
- The user needs a macro that identifies both workbooks rather than relying only on the active workbook.
Open the source workbook and identify its file name, worksheet name, and the range to copy. If you plan to save the macro in a workbook, save that workbook as an Excel Macro-Enabled Workbook (.xlsm).
Record and edit a macro to copy data into a new workbook
Use the recorder to capture the workflow, then edit the VBA to identify the source explicitly and create a destination workbook.
In Excel, select View > Macros > Record Macro. Choose where to store the macro, enter a name, and select OK.
Switch to the open source workbook, select the data range, and copy it. Create a new workbook, paste the range into cell A1, and select View > Macros > Stop Recording.
Open View > Macros > View Macros, select the recorded macro, and choose Edit. Replace its code with the following example, changing the source workbook name, sheet name, and range: Sub CopyDataToNewWorkbook() Dim source As Workbook Dim destination As Workbook Set source = Workbooks("Source.xlsx") Set destination = Workbooks.Add source.Worksheets("Sheet1").Range("A1:D10").Copy Destination:=destination.Worksheets(1).Range("A1") End Sub
With Source.xlsx open, run the macro from View > Macros > View Macros. In the new workbook, choose File > Save As to save the copied data.

Copy a sparkline range as a picture into a new workbook
Use CopyPicture when you want a visual snapshot of cells containing sparklines rather than editable cells.
Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface for opening and editing Excel files. Its format compatibility helps you move existing spreadsheets into WPS; check macro behavior separately before relying on an Excel VBA workflow.

Frequently Asked Questions
Can a recorded Excel macro work with more than one workbook?
Yes. Record the switches and copy actions, then use View > Macros > View Macros > Edit to check that the VBA references the intended source and destination workbooks.
Why does Workbooks("Source.xlsx") fail?
The named workbook must already be open, and its name must match exactly. Check the workbook name in Excel and update the Workbooks reference in the VBA editor.
How do I copy into the workbook that contains the macro?
In the VBA editor, set the destination to ThisWorkbook instead of Workbooks.Add. Specify the destination worksheet and cell in the Copy statement.
Can I paste sparklines as an image?
Yes. Use Range.CopyPicture on the cells containing the sparklines, then paste into the destination worksheet. The result is a static picture, not an editable sparkline.
Where should I save the macro?
When you start recording through View > Macros > Record Macro, choose a storage location. If you store it in a workbook, use File > Save As and select Excel Macro-Enabled Workbook (.xlsm).




