VBA 剪切和粘贴错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13323915/
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 14:23:31  来源:igfitidea点击:

VBA Cut and Paste error

excelexcel-vbavba

提问by Anthony

I'm trying to cut the contents of cell K7(100) and paste it into M7using VBA (see below) but I keep getting an error (see below). Where am I going wrong?:

我正在尝试使用 VBA(见下文)剪切单元格K7(100)的内容并将其粘贴到M7中(见下文),但我不断收到错误(见下文)。我哪里出错了?:

Sub CutPaste()    
    Worksheets("Sheet2Test").Activate
    Range("K7").Select
    Selection.Cut
    Range("M7").Select
    Selection.Paste   
End Sub

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答by chris neilsen

Its better to avoid Selectaltogether. Use this

最好Select完全避免。用这个

Worksheets("Sheet2Test").Range("K7").Cut Worksheets("Sheet2Test").Range("M7")

回答by Nelson

Just replace Selection.Pastefor ActiveSheet.Paste, so it would be:

只需替换Selection.PasteActiveSheet.Paste,所以它将是:

Sub CutPaste()    
    Worksheets("Sheet2Test").Activate
    Range("K7").Select
    Selection.Cut
    Range("M7").Select
    ActiveSheet.Paste
End Sub

That would do the paste as you wanted.

这将按照您的意愿进行粘贴。