VBA & Macro Problems

How to Fix VBA Paste Errors When Copying Data Between Excel Workbooks

Kushani NimanthikaKushani Nimanthika Nov 2, 2026 870 views

Question details

Users experience paste errors or missing values when using a VBA macro to copy data from one workbook to another, often caused by clipboard clearing upon closing the source file or incorrect PasteSpecial settings.

How to Fix VBA Paste Errors When Copying Data Between Excel Workbooks
Product
Excel
Device & OS
not provided
Scenario
Automating data transfer between two different workbooks using a VBA macro.
Observed behavior
The macro fails to paste the copied values, pastes only formatting instead of data, or throws a runtime error because the clipboard is emptied when the source workbook is closed.
Before you start

Ensure both the source and target workbooks are accessible, and verify that the data ranges you intend to transfer have exactly the same dimensions (number of rows and columns).

Solution 1Recommended

Use Direct Range Assignment to Transfer Values

Assigning the values of one range directly to another is faster and avoids clipboard-related paste errors entirely.

When you use standard Copy and Paste methods in VBA, the system clipboard is utilized. If the source workbook is closed before the paste operation completes, the clipboard clears, causing the macro to fail. Additionally, using PasteSpecial with incorrect parameters (such as xlPasteFormats) will result in missing data. Direct range assignment bypasses the clipboard completely, offering a cleaner and more efficient execution.

1
Open the Visual Basic Editor

Press Alt + F11 on your keyboard to open the Visual Basic Editor (VBE) in your workbook.

2
Locate the macro code

In the Project Explorer pane on the left, double-click the Module containing your data-fetching macro.

3
Replace Copy/Paste with direct assignment

Remove the '.Copy' and '.PasteSpecial' lines. Replace them with a direct assignment where the target range equals the source range. For example: targetSheet.Range("B3:AF1002").Value = sourceBook.Worksheets(1).Range("A1:AE1000").Value

4
Run and test the macro

Save your code and press F5 or return to your workbook to run the macro. The data will transfer instantly without utilizing the clipboard.

Use Direct Range Assignment to Transfer Values
Transferring Formulas: If your source range contains formulas that you need to carry over instead of just their calculated results, change '.Value' to '.Formula' in your assignment code (e.g., TargetRange.Formula = SourceRange.Formula).
Automate with WPS

Run VBA Macros Smoothly with WPS Spreadsheet

WPS Office provides robust support for VBA and macros, allowing you to automate tasks and transfer data between workbooks seamlessly. It offers high compatibility with existing Microsoft Excel macro scripts.

  1. 1. Open WPS Spreadsheet: Launch WPS Office and open your macro-enabled workbook (.xlsm).
  2. 2. Access the Developer Tools: Navigate to the 'Tools' tab on the top ribbon and click on 'Developer' to access the VBA Editor.
  3. 3. Insert your Macro: Insert a new Module, paste your optimized direct range assignment VBA code, and run it to instantly transfer data.
Fully supports VBA and Excel macro scripts nativelyHigh compatibility with Microsoft Excel formats (.xlsm, .xlsx)Lightweight and fast execution for automated data processing
microsoft office alternative - wps office

Frequently Asked Questions

Why does my VBA code only paste formatting and not values?

This usually happens if your PasteSpecial method is set to Paste:=xlPasteFormats. To fix this, change the parameter to Paste:=xlPasteValues, or bypass the clipboard entirely by using direct range assignment (TargetRange.Value = SourceRange.Value).

Why do I get a paste error when closing the source workbook in VBA?

When a workbook is closed via VBA (e.g., sourceBook.Close), Excel clears the system clipboard. If you copied data but haven't pasted it before the close command executes, there is nothing left to paste, resulting in an error. Direct assignment solves this by pulling the data before the workbook closes.

Can I transfer data of different range sizes using direct assignment?

No. For direct range assignment to work correctly, both the source and target ranges must have the exact same dimensions (the identical number of rows and columns). If the dimensions mismatch, you may encounter missing data or #N/A errors.