“粘贴”运行时出现 VBA 运行时错误 438

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22193723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:54:47  来源:igfitidea点击:

VBA Run-time error 438 appears when "paste" runs

excelvbaexcel-vbapaste

提问by Seya

I'm just getting familiar with VBA and my code

我刚刚熟悉 VBA 和我的代码

For k = 3 To ThisWorkbook.Sheets.Count
ThisWorkbook.Sheets(k).Activate
ActiveSheet.Cells(11, 2).Select

lLastRow = Cells(Rows.Count, 1).End(xlUp).Row
ActiveSheet.Range("A5:" & "A" & CStr(lLastRow)).Copy
' ThisWorkbook.Sheets(1).Cells("B" & CStr(lFirstRow) & ":B" & CStr(lLastRow)).Select
ThisWorkbook.Sheets(1).Activate
ActiveSheet.Cells(lFirstRow, 2).Select
Selection.Paste
'Selection.PasteSpecial Paste:=xlPasteValues
'Selection.PasteSpecial Paste:=xlPasteFormats
lFirstRow = lFirstRow + lLastRow
Next k

makes "Run-time error 438. Object doesn't support this porperty or method" to appear when the line "Selection.Paste" goes. What's the problem?:( I've tried to use paste special, to activate sheet and to select cell (not range), to use Cstr, but nothing changed

使“运行时错误 438。对象不支持此属性或方法”在“Selection.Paste”行出现时出现。有什么问题?:(我尝试使用特殊粘贴,激活工作表并选择单元格(不是范围),使用 Cstr,但没有任何改变

回答by Mark Fitzgerald

Try Selection.PasteSpecial xlPasteAll

尝试 Selection.PasteSpecial xlPasteAll

Pasteby itself works on several objects, most notably Worksheetbut not on a Rangeobject which is what your Selectionis.

Paste本身适用于多个对象,最显着的是Worksheet但不适用于Range您所在的对象Selection

To paste to a Rangeyou really have to use the PasteSpecialmethod with its' available arguements such as xlPasteAll; xlPasteValues; xlPasteFormulas; xlPasteFormatsand others which you can see by pressing F1while the cursor is within PasteSpecialin the VBE.

要粘贴到 a,Range您确实必须使用该PasteSpecial方法及其可用的论点,例如xlPasteAll; xlPasteValues; xlPasteFormulas; 当光标位于 VBE 中时xlPasteFormats,您可以通过按来查看其他内容。F1PasteSpecial

回答by Ravi S Patidar

Replace these two lines in your code

替换代码中的这两行

ActiveSheet.Cells(lFirstRow, 2).Select
Selection.Paste

by

经过

Cells(lFirstRow, 2).Select
Activesheet.paste

your code will work flawlessly

您的代码将完美运行



Important note for working with paste and pastespecial in vba

在 vba 中使用 paste 和 pastespecial 的重要说明

Copy any range from anywhere then

然后从任何地方复制任何范围

  1. Paste Special method (Sheets.Cells/Range.PasteSpecial)

    Sheets ("Daily Shortage").Activate

    Sheets ("Daily Shortage").Cells (m, 1). PasteSpecial Paste: = xlPasteValues

  1. 粘贴特殊方法 ( Sheets.Cells/Range.PasteSpecial)

    床单(“每日短缺”)。激活

    床单(“每日短缺”)。单元格(m,1)。PasteSpecial Paste: = xlPasteValues

One Example –

一个例子——

Will throw error

会抛出错误

Sheets ("June"). Range ("A10").Select

ActiveSheet.PasteSpecial Paste: = xlPasteValues

This will work flawlessly

这将完美无缺地工作

Sheets ("June"). Range ("A10").PasteSpecial Paste: = xlPasteValues
  1. Paste method (ActiveSheet.Paste)

    Sheets ("June"). Range ("A10").Select

    ActiveSheet.Paste

  1. 粘贴法 ( ActiveSheet.Paste)

    表(“六月”)。范围(“A10”)。选择

    ActiveSheet.粘贴