Paste special in VBA

oerwouter

New Member
Joined
Dec 4, 2011
Messages
21
Hi,
I want to make a macro to just paste values for cells that have been copied manually by the user in another workbook.

Code:
Sub PasteValues()

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False

End Sub

However I get the 1004 error: "PasteSpecial method of range class failed"

Strangest thing, because it works in another Excel file I made, I copied the code from there.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I have just had a play with this and I too could not get it to work until I put the line selection.copy at the top, It must need to follow a copy command
 
Upvote 0
You don't need to use a macro.
Excel has a built-in Paste-Values Command/Button
-- removed inline image ---

Select and put it in the command Bar.

Or try a simplified version:
Sub PasteValues()
ActiveSheet.PasteSpecial Paste:=xlPasteValues
End Sub
 
Upvote 0
I have just had a play with this and I too could not get it to work until I put the line selection.copy at the top, It must need to follow a copy command
You may have mis-interpreted this bit:
Hi,
.. for cells that have been copied manually by the user in another workbook.
As I understand that (and tested as such), the user has already (manually) selected and copied some cells in a (different) workbook, then changed workbooks, made a selection then run the code. So a 'Copy' had already been done, it just wasn't done by this code.
 
Upvote 0
I have just had a play with this and I too could not get it to work until I put the line selection.copy at the top, It must need to follow a copy command

In VBA, the PasteSpecial command/method can only paste from the Clipboard, so if some other action is clearing the Clipboard between the Copy and PasteSpecial actions it won't work.

So yes, you must have a Copy method on a separate code line immediately before the PasteSpecial method
 
Upvote 0
In VBA, the PasteSpecial command/method can only paste from the Clipboard, so if some other action is clearing the Clipboard between the Copy and PasteSpecial actions it won't work.

So yes, you must have a Copy method on a separate code line immediately before the PasteSpecial method
I agree that the if the clipboard is empty then the code will fail with the error reported, so that is the likely problem.

I disagree with the blue bit. Putting something on the clipboard some other way (eg via a manual Copy before running the code) works fine.
 
Upvote 0
A bit of experiments brought out the following:
After a manual copy operation,

Sheet version of PasteSpecial,
ActiveSheet.PasteSpecial Paste:=xlPasteValues fails with Runtime 1004 error.

But the Range version,
ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteValues works fine

Range version with activecell reference,
ActiveSheet.ActiveCell.PasteSpecial Paste:=xlPasteValues fails with Runtime 438 error.

But Range version using selection,
Selection.PasteSpecial Paste:=xlPasteValues works fine.


I think the last version is the most suitable single line solution in the current context.

Sub PasteValues()
Selection.PasteSpecial Paste:=xlPasteValues
End Sub
 
Upvote 0
But Range version using selection,
Selection.PasteSpecial Paste:=xlPasteValues works fine.


I think the last version is the most suitable single line solution in the current context.

Sub PasteValues()
Selection.PasteSpecial Paste:=xlPasteValues
End Sub
Isn't that what the OP is using?
 
Upvote 0
Isn't that what the OP is using?
You are right!
I double checked Selection.PasteSpecial Paste:=xlPasteValues after a manual copy.
It is NOT erroring out in the same sheet, different sheet or even different workbook.
OP to comment.
 
Upvote 0
[/B]It is NOT erroring out in the same sheet, different sheet or even different workbook.
It does if the copy gets cancelled between the copy process and the macro (eg press Esc key). I suspect somehow that is happening to the OP (could be other code?).
 
Upvote 0

Forum statistics

Threads
1,223,723
Messages
6,174,121
Members
452,545
Latest member
boybenqn

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