Home » excel » excel – Macro to insert rows and fill is failing

excel – Macro to insert rows and fill is failing

Posted by: admin May 14, 2020 Leave a comment


I’ve written the following macro to insert a specified number of rows into a table, and then to filldown, to re-populate the table with the correct forumulas.

I don’t know why this is failing with an error at ActiveSheet.Rows(r).Insert with runtime error 1004. Insert method of Range class failed.

Could you help me out?

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


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

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

While i < Duration



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

Application.CutCopyMode = False

ScreenUpdating = True

End Sub
How to&Answers:

Try fully qualifying your ranges with full workbooks.worksheets.range paths.


It looks like your line below might not be returning the result you want:

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

if i is less than 18, you’ll get an infinite loop here:

While i < Duration



…which eventually results in your 1004 error