vba error 1004 PasteSpecial method of Range class failed with pastespecial

ww4612

Well-known Member
Joined
Apr 24, 2014
Messages
515
Hello
I wrote a vba code to copy data from one sheet to another sheet. the code is like:

Sub tt()
Dim a As Long
finalcolumn = Cells(1, Columns.Count).End(xlToLeft).Column
For a = 1 To Columns.Count
Sheets("sheet2").Select
If Cells(1, a).Value <> 0 Then
'copy the first 3 columns of the first 5 rows in sheet2, if the first cell is not empty
Range(Cells(1, a), Cells(5, a + 2)).Copy
Sheets("sheet1").Select
'paste the copied value to the same cells in sheet1
Cells(1, a).PasteSpecial Paste = xlPasteValues
finalcolumn = finalcolumn + 3
a = a + 3
Else
'if the first cell in the range Cells(1, a), Cells(5, a + 2) in sheet 2 is empty. end the loop
Exit For
End If
Next a
End Sub

the error check keep highlight the following code with error 1004 PasteSpecial method of Range class failed
Cells(1, a).PasteSpecial Paste = xlPasteValues
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Please, use special CODE tags for code and make indentations. Your code is barely readable.
Instead of
Code:
[COLOR=#333333][FONT=Verdana]Sheets("sheet1").Select[/FONT][/COLOR]
[COLOR=#333333][FONT=Verdana]Cells(1, a).PasteSpecial Paste = xlPasteValues[/FONT][/COLOR]
you can write:
Code:
[COLOR=#333333][FONT=Verdana]Sheets("sheet1").Cells[/FONT][/COLOR][COLOR=#333333][FONT=Verdana](1, a).PasteSpecial Paste:=xlPasteValues[/FONT][/COLOR]
 
Upvote 0
thank you. it actually worked. that was a stupid mistake. by the way I am new to vba. so I don't really know what is CODE tag. Can someone explain what it is?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,086
Messages
6,123,038
Members
449,092
Latest member
ikke

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