vba Excel 2010 中的错误,范围类的 PasteSpecial 方法在宏中失败

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

Error in Excel 2010, PasteSpecial method of Range Class failed within macro

excelexcel-vbaexcel-2010vba

提问by user1382191

Following is my code that's throwing this error:

以下是我抛出此错误的代码:

PasteSpecial method of Range class failed

Range 类的 PasteSpecial 方法失败

Error is throwing only when I'm trying to debug.

只有在我尝试调试时才会抛出错误。

Range(Cells(7, 1), Cells((Rowrange + 7), 2)).Select
Selection.PasteSpecial Paste:=xlValues


  ' my complete code

strSheetName = "sheet1"
Sheets(strSheetName).Select
B6 = Range("B6").Value
B7 = Range("B7").Value
Range(Cells(11, 1), Cells((Rowrange + 11), 2)).Select
Selection.Copy

strSheetName = "sheet2"
Sheets(strSheetName).Select
' Range(Cells(7, 1), Cells((Rowrange + 7), 2)).Select
'.Range(Cells(7,1), .Cells(RowRange + 7, 2). PasteSpecial Paste := xlValues
'Selection.PasteSpecial Paste:=xlValues
With ActiveSheet
.Range(.Cells(7, 1), .Cells(Rowrange + 7, 2)).PasteSpecial Paste:=xlValues
End With

Is there a way to avoid this error?

有没有办法避免这个错误?

采纳答案by Siddharth Rout

I believe (If the above is actually your complete code) you are not copying the data and directly trying to do a paste and hence you are getting that error :)

我相信(如果以上实际上是您的完整代码)您没有复制数据并直接尝试进行粘贴,因此您会收到该错误:)

Is this what you are trying?

这是你正在尝试的吗?

strSheetName = "Estimated vs Actual Costs"
With Sheets(strSheetName)
    .Range(.Cells(7, 1), .Cells(RowRange + 7, 2)).Copy
    .Range(.Cells(7, 1), .Cells(RowRange + 7, 2)).PasteSpecial Paste:=xlValues
End With

FOLLOWUP

跟进

Try this

尝试这个

strSheetName = "sheet1"
With Sheets(strSheetName)
    B6 = .Range("B6").Value
    B7 = .Range("B7").Value
    .Range(.Cells(11, 1), .Cells((RowRange + 11), 2)).Copy
End With

strSheetName = "sheet2"
With Sheets(strSheetName)
    .Range(.Cells(7, 1), .Cells(RowRange + 7, 2)).PasteSpecial Paste:=xlValues
End With

回答by assylias

The best way to avoid that kind of error is to only use fully qualified ranges:

避免这种错误的最好方法是只使用完全限定的范围:

With Sheets("The name of the sheet")
    .Range(.Cells(7, 1), .Cells(RowRange + 7, 2)).PasteSpecial Paste:=xlValues
End With

Or: With ActiveSheetif you know the sheet is already selected.

或者:With ActiveSheet如果您知道工作表已被选中。

Also note that selecting is not necessary.

另请注意,选择不是必需的。