Excel 2010中的错误,Range类中的PasteSpecial方法在macros内失败

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

Range类的PasteSpecial方法失败

错误只在我试图debugging的时候才抛出。

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 

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

我相信(如果上述其实是你的完整的代码),你不是复制数据,直接试图做一个粘贴,因此你得到的错误:)

这是你正在尝试?

 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 

跟进

尝试这个

 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 

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

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

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

另外请注意,select是没有必要的。