Home » excel » Excel VBA PasteSpecial xlPasteValues Syntax error

Excel VBA PasteSpecial xlPasteValues Syntax error

Posted by: admin May 14, 2020 Leave a comment


I’m trying to copy data from one .xlsx file to my .xlsm file, but without the formatting. When I try to use the following code line an unspecified Syntax error appears.

TargetSheet.Range(“A” & TargetLastRow).PasteSpecial xlPasteValues (or various other forms)

Sub Import_Click()

'Set the target workbook for import.'
Dim TargetWorkbook As Workbook
Set TargetWorkbook = Application.ActiveWorkbook

'Prompt window for user to select and import file.'
Dim UserFilename As String
UserFilename = Application.GetOpenFilename

Dim UserWorkbook As Workbook
Set UserWorkbook = Application.Workbooks.Open(UserFilename)

'Copy data from source to target sheet.'
Dim SourceSheet As Worksheet
Set SourceSheet = UserWorkbook.Worksheets(1)

Dim TargetSheet As Worksheet
Set TargetSheet = TargetWorkbook.Worksheets(1)

Dim SourceLastRow As Long
SourceLastRow = SourceSheet.Cells(SourceSheet.Rows.Count, "A").End(xlUp).Row

Dim TargetLastRow As Long
TargetLastRow = TargetSheet.Cells(TargetSheet.Rows.Count, "A").End(xlUp).Offset(1).Row

SourceSheet.Range("A2:S" & SourceLastRow).Copy _
TargetSheet.Range("A" & TargetLastRow)

'Close import workbook.'

End Sub
How to&Answers:

Either you copy in onle line by giving the destination as first parameter of .Copy

SourceSheet.Range("A2:S" & SourceLastRow).Copy Destination:=TargetSheet.Range("A" & TargetLastRow)

Or if you want to use SpecialPaste you need to do it in 2 seperate statements:

SourceSheet.Range("A2:S" & SourceLastRow).Copy 'no destination given here
'separate statement for pasting:
TargetSheet.Range("A" & TargetLastRow).PasteSpecial xlPasteValues

note that there must be no _ as line concatenater at the end of the .Copy line then!


I would try to avoid using Copy and Paste, replacing of the lines

SourceSheet.Range("A2:S" & SourceLastRow).Copy _
TargetSheet.Range("A" & TargetLastRow)

With the next code

TargetSheet.Range("A" & TargetLastRow).Resize(SourceLastRow - 1, _
        SourceSheet.Range("A2:S" & SourceLastRow).Columns.count) = _
                        SourceSheet.Range("A2:S" & SourceLastRow)