macros在Excel 2013中不起作用

我有一些macros在Excel 2003中设置,然后在Excel 2010中继续工作。但是,现在我已经移动到Excel 2013,他们无法正常工作。 错误是:

运行时错误“-2147417848(80010108)”:对象'范围'的方法'插入'失败

其中一个macros是以下内容:

'Unprotect Sheets("Appraisal").Unprotect "*********" Dim x As Integer Dim y As Integer Dim z As Integer x = Range("cashflowformulas:endcashflow").Columns.Count y = Range("projectduration").Value If x - 1 < y Then If y - x > 10 Then Do z = z + 1 'Copy first column of cashflow formulas Range("cashflowformulas").Copy 'Insert copied cells at end of cashflow to give additional project months Range("endcashflow:offset(endcashflow,,10-1)").Insert shift:=xlToRight Loop Until z > (y - x) / 10 - 1 End If 'Copy first column of cashflow formulas Range("cashflowformulas").Copy 'Insert copied cells at end of cashflow to give additional project months Range("endcashflow:offset(cashflowformulas,,sum(projectduration)-1)").Insert shift:=xlToRight ElseIf y > 0 Then If x - 1 > y Then If MsgBox("Cashflow data beyond the new project duration will be permanently deleted. Do you want to proceed?", vbYesNo) = vbYes Then 'Remove cashflow months after project end date Range("offset(endcashflow,,-1):offset(cashflowformulas,,sum(projectduration))").Delete Else Range("projectduration").Formula = x - 1 MsgBox "Operation cancelled" End If End If End If 'Reprotect Sheets("Appraisal").Protect "simonproof", DrawingObjects:=True, Contents:=True, Scenarios:=True End Sub 

"ProjectDuration"是单个单元格,而其他命名范围是列,例如"Appraisal!$S:$S"

我试图改变Insert shift:=xlToRight Insert shift:=xlShiftRightInsert shift:=xlDown Insert shift:=xlShiftDown但在debugging我仍然陷入:

 Range("endcashflow:offset(cashflowformulas,,sum(projectduration)-1)").Insert shift:=xlShift50Right 

我该如何解决?

更改shift:=xlToRight(xlToRight)

请参阅下面的新代码

 'Unprotect Sheets("Appraisal").Unprotect "*********" Dim x As Integer Dim y As Integer Dim z As Integer x = Range("cashflowformulas:endcashflow").Columns.Count y = Range("projectduration").Value If x - 1 < y Then If y - x > 10 Then Do z = z + 1 'Copy first column of cashflow formulas Range("cashflowformulas").Copy 'Insert copied cells at end of cashflow to give additional project months Range("endcashflow:offset(endcashflow,,10-1)").Insert (xlToRight) Loop Until z > (y - x) / 10 - 1 End If 'Copy first column of cashflow formulas Range("cashflowformulas").Copy 'Insert copied cells at end of cashflow to give additional project months Range("endcashflow:offset(cashflowformulas,,sum(projectduration)-1)").Insert (xlToRight) ElseIf y > 0 Then If x - 1 > y Then If MsgBox("Cashflow data beyond the new project duration will be permanently deleted. Do you want to proceed?", vbYesNo) = vbYes Then 'Remove cashflow months after project end date Range("offset(endcashflow,,-1):offset(cashflowformulas,,sum(projectduration))").Delete Else Range("projectduration").Formula = x - 1 MsgBox "Operation cancelled" End If End If End If 'Reprotect Sheets("Appraisal").Protect "simonproof", DrawingObjects:=True, Contents:=True, Scenarios:=True End Sub 

我遇到了同样的事情,并在这里find了解决办法

显然,在清除任意单元的复制命令之前添加一行可以解决问题。

 'Dummy clear statement: ActiveSheet.Cells(1, 1).Clear 'Copy first column of cashflow formulas Range("cashflowformulas").Copy 'Insert copied cells at end of cashflow to give additional project months Range("endcashflow:offset(endcashflow,,10-1)").Insert shift:=xlToRight