
Hi @Jake Guy,
Thank you for posting your question in the Microsoft Q&A forum.
Based on my research, here are some common reasons why the Runtime Error 1004 might occur during the AutoFill operation in your macro:
- Source cell (I38) lacks a formula: If I38 is blank or contains a static value, Excel doesn’t know what to autofill, which causes the error. → Make sure I38 contains a valid formula before running the macro.
- Selection context issue: Using Range("I38").Select followed by Selection.AutoFill only works if I38 is actively selected and contains a formula. → Use a direct reference instead: Range("I38").AutoFill Destination:=Range("I38:I40")
- Target range inconsistency: If row 40 doesn’t exist yet or the range isn’t properly formatted, Excel may throw an error. → Ensure rows 38 to 40 exist and are ready for autofill.
Here is a modified version of the macro with more secure autofill logic that you can try:
Sub Insert_DATA_Row()
' Insert a new row at 39
Rows("39:39").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
' Autofill A and B columns
Range("A38:B38").AutoFill Destination:=Range("A38:B40"), Type:=xlFillDefault
' Ensure I38 has a formula before autofill
If Range("I38").HasFormula Then
Range("I38").AutoFill Destination:=Range("I38:I40"), Type:=xlFillDefault
Else
MsgBox "Cell I38 must contain a formula before autofill."
End If
' Clear contents in E to J for the new row
Range("E39:J39").ClearContents
End Sub
Please let me know if you run into any issues or have updates. And in case I misunderstood your original question or intent, please feel free to clarify. We can work together to find a better solution if necessary. Your reply is valuable, and I’m here to support you through the process.
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.