Excel VBA PasteSpecial xlPasteValues 语法错误

标签 excel vba

我正在尝试将数据从一个 .xlsx 文件复制到我的 .xlsm 文件,但没有格式化。当我尝试使用以下代码行时,会出现未指定的语法错误。

TargetSheet.Range("A"& TargetLastRow).PasteSpecial xlPasteValues(或其他各种形式)

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.'
UserWorkbook.Close

End Sub

最佳答案

您可以通过将目的地作为.Copy 的第一个参数来复制一行。

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

或者,如果您想使用 SpecialPaste,您需要在 2 个单独的语句中执行此操作:
SourceSheet.Range("A2:S" & SourceLastRow).Copy 'no destination given here
'separate statement for pasting:
TargetSheet.Range("A" & TargetLastRow).PasteSpecial xlPasteValues

注意不能有_作为 .Copy 末尾的行连接符那就行!

关于Excel VBA PasteSpecial xlPasteValues 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60282167/

相关文章:

excel - 使用宏从 Excel 电子表格中删除任何非指定字符

多个匹配项的 Excel 条件格式

excel - 强制 Excel 退出编辑模式的宏

vba - 如何动态更改公式内的范围?

arrays - VBA将数组缓慢写入Excel工作簿

javascript - 仅使用 javascript 和 html 读取 csv 或 excel (xlsx) 文件?

vba - Excel 中的 Munging 时间序列

vba - 在文件夹中搜索关键字

excel - 范围内的最后一行

当系统设置为欧洲日期格式时,Excel 搞乱了我的日期