Macro Runtime error 1004 "autofill" issue

Jake Guy 0 Reputation points
2025-08-18T11:59:38.8366667+00:00

image

So, i have a macro on a certificate generator, it adds rows to the "as found" table, as seen in the attached picture the "testpoint","set point" and "deviation" columns have formulas for the data. Below is the code used by the "ADD AS FOUND DATA ROW", but i get a runtime error on the autofill line for I38:I40, is there any advice or fix? the max that this table will be is 5 rows.

Sub Insert_DATA_Row()

' Insert_DATA_Row Macro

Rows("39:39").Select

Selection.Copy

Selection.Insert Shift:=xlDown

Range("A38:B38").Select

Application.CutCopyMode = False

Selection.AutoFill Destination:=Range("A38:B40"), Type:=xlFillDefault

Range("I38").Select

Application.CutCopyMode = False

Selection.AutoFill Destination:=Range("I38:I40"), Type:=xlFillDefault

Range("E39:J39").Select

Selection.ClearContents

Range("E39:G39").Select

End Sub

Microsoft 365 and Office | Excel | For education | Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rin-L 3,045 Reputation points Microsoft External Staff Moderator
    2025-08-18T13:12:53.4566667+00:00

    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. 


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.