Run-time error 1004 when trying to add new row to table

perezpa

New Member
Joined
Jun 17, 2011
Messages
17
I am getting a run-time error 1004 (This operation is attempting to shift cells in a table on your worksheet) when I try to insert a row, shift information down, and paste the copied selection in the inserted row. I don't know why this is an error if this is exactly what I want to do.

Here is the code for reference. The error occurs in the If statement located between the equal-sign dividers.

Code:
Option Explicit
Sub Actualizar_informe()
Dim folderPath As String
Dim fileName As String
Dim WB As Workbook
Dim thisWB As Workbook
Dim wsdata As Object
Dim MyYear As Integer
Dim MyMonth As Byte
Set thisWB = ThisWorkbook
Set wsdata = thisWB.Worksheets("data")
Range("A5:J5").End(xlDown).ClearContents

    
    folderPath = "Z:\Incentivos\Electromuebles\Consolidados\Meses Anteriores\Desde 2008"
    
    If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
    
    fileName = Dir(folderPath & "*.xls")
    Do While fileName <> ""
        MyYear = CInt(Left(fileName, 4))
        MyMonth = CByte(Mid(fileName, 5, 2))
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        Application.EnableEvents = False
        Set WB = Workbooks.Open(folderPath & fileName, UpdateLinks:=False, ReadOnly:=True)
        
        '==================================================================================================================
        Dim row As Integer
        Dim wsRes As Object
        Set wsRes = WB.Worksheets("Resumen")
                
                           
            For row = 1 To 25
                If Left(wsRes.Cells(row, 1).Value, 2) = "MC" Or Left(wsRes.Cells(row, 1).Value, 2) = "LS" Then
                    wsRes.Cells.Range("A" & row, "M" & row).Copy
                    Rows("5:5").Insert shift:=xlDown
                    wsdata.Rows("5:5").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=True, Transpose:=False
                    wsdata.Range("N5").Value = MyYear
                    wsdata.Range("O5").Value = MyMonth
                End If
            Next row
        '==================================================================================================================
        
        WB.Close
        fileName = Dir
    Loop
    
    wsdata.Columns("E:E").Delete shift:=xlToLeft
End Sub

Thank you in advance.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top