VBA Excel 2010 中粘贴特殊错误

Paste Special error in VBA Excel 2010

我正在尝试复制和粘贴值,这实际上并不难。

为了只复制单元格的值,我使用 Paste.Special x1PasteValues。

有趣的部分来了:

Set wsd = Sheets("Data")
wsd.Select
With wsd
.Range("L5:X5").Select

.PasteSpecial x1=PasteValues        <~~ Runtime error: 1004        
'.PasteSpecial Paste:=xlPasteValues <~~ equivalent to the above
'.PasteSpecial                      <~~ This works, but only pastes the formula
'.Paste                             <~~ This pastes the formula

End With

如果我执行代码,我会收到运行时错误:1004,而且我找不到解决方案。如果我想粘贴格式,也会发生同样的情况。

可能问题是宏是写在ActiveX按钮里的,这就是为什么我必须用wsd的原因。

非常感谢您的帮助。提前致谢!

编辑:我刚刚发现了一个类似的主题,其中通过切换工作簿并手动激活宏(而不是宏按钮)出现了问题。由于我在工作表之间切换,我的代码中会出现同样的问题吗?

Excel VBA runtime error 1004

看起来您正在引用 sheet,选择单元格,然后再次引用 sheet 对象以粘贴到其中,而没有给出这样做的范围。这适用于 Excel 2013,您可能需要使用其他版本的语法

Set wsd = Sheets("Data")
With wsd
    .Range("L5:X5").PasteSpecial xlPasteValues, xlPasteSpecialOperationNone, False, False
End With