Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

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

I'm just getting familiar with VBA and my code

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try 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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...