插入行和填充的宏失败

时间:2018-11-08 14:22:13

标签: excel vba insert runtime-error

我编写了以下宏,以便在表中插入指定数量的行,然后进行填充,以使用正确的论坛名重新填充表。

我不知道为什么这会因ActiveSheet.Rows(r)错误而失败。插入时发生运行时错误1004。Range类的插入方法失败。

你能帮我吗?

Sub SetKPIDuration()

Dim Duration As Integer, i As Integer, r As Integer

Duration = InputBox("Enter number of week for KPI to run (min 18)", 
"Duration of KPI", 18)

Select Case True
    Case Duration < 10
        Duration = 18
        GoTo IncreaseKPI
    Case Duration < Application.WorksheetFunction.Max(Range("A7:A150"))
        GoTo ReduceKPI
    Case Else
        GoTo IncreaseKPI
End Select

ReduceKPI:

Rows((Duration + 7) & ":150").Clear
Exit Sub

IncreaseKPI:
Application.ScreenUpdating = False
i = Application.WorksheetFunction.Max(Range("A7:A150"))
r = i + 7

While i < Duration

    ActiveSheet.Rows(r).insert

Wend

Range("A" & (r - 1) & ":" & "M" & (r + i)).filldown


Application.CutCopyMode = False


ScreenUpdating = True

End Sub

2 个答案:

答案 0 :(得分:1)

尝试使用完整的workbooks.worksheets.range路径完全限定范围。

答案 1 :(得分:1)

您的下面的代码行可能未返回您想要的结果:

i = Application.WorksheetFunction.Max(Range("A7:A150"))

如果i小于18,则会在此处出现无限循环:

While i < Duration

    ActiveSheet.Rows(r).insert

Wend

...最终会导致您1004错误