“粘贴”运行时出现 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
VBA Run-time error 438 appears when "paste" runs
提问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
Paste
by itself works on several objects, most notably Worksheet
but not on a Range
object which is what your Selection
is.
Paste
本身适用于多个对象,最显着的是Worksheet
但不适用于Range
您所在的对象Selection
。
To paste to a Range
you really have to use the PasteSpecial
method with its' available arguements such as xlPasteAll
; xlPasteValues
; xlPasteFormulas
; xlPasteFormats
and others which you can see by pressing F1
while the cursor is within PasteSpecial
in the VBE.
要粘贴到 a,Range
您确实必须使用该PasteSpecial
方法及其可用的论点,例如xlPasteAll
; xlPasteValues
; xlPasteFormulas
; 当光标位于 VBE 中时xlPasteFormats
,您可以通过按来查看其他内容。F1
PasteSpecial
回答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
然后从任何地方复制任何范围
Paste Special method (
Sheets.Cells/Range.PasteSpecial
)Sheets ("Daily Shortage").Activate
Sheets ("Daily Shortage").Cells (m, 1). PasteSpecial Paste: = xlPasteValues
粘贴特殊方法 (
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
Paste method (
ActiveSheet.Paste
)Sheets ("June"). Range ("A10").Select
ActiveSheet.Paste
粘贴法 (
ActiveSheet.Paste
)表(“六月”)。范围(“A10”)。选择
ActiveSheet.粘贴