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
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