Code throwing 1004 Error

Terry Echols

New Member
Joined
Jul 14, 2015
Messages
38
This code has worked for years until recently (this month). Now all of a sudden it is throwing the dreaded Runtime Error 1004 "Application-defined or Object-defined error"

I have tried everything I can think of but the error keeps showing up every time I run the code. Can anyone shed some light on this error from the code below, please.

I'm using Office 365

Code:
Sub DataExtract()

With Application
 .DisplayAlerts = False
 .ScreenUpdating = False
End With

Dim i As Long
Dim j As Long
Dim k As Long
Dim objFSO As Object
Dim objFolder As Object
Dim objFile  As Object
Dim wb As Workbook

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(ThisWorkbook.Path & "\Statements\")

For Each objFile In objFolder.Files
    If InStr(objFile, ".xls") Then
        Workbooks.Open (objFile)
    End If
    Set wb = ActiveWorkbook
    i = Sheet1.Cells(Rows.Count, "B").End(xlUp).Row + 1
    j = wb.Sheets("Statement").Cells(Rows.Count, "C").End(xlUp).Row
    If wb.Sheets("Statement").Range("C13") <> vbNullString Then
        wb.Sheets("Statement").Range("A13:F" & j).Copy
        Sheet1.Range("B" & i).PasteSpecial xlPasteValuesAndNumberFormats
        wb.Sheets("Statement").Range("I13:I" & j).Copy
        Sheet1.Range("H" & i).PasteSpecial xlPasteValuesAndNumberFormats
        k = Sheet1.Cells(Rows.Count, "B").End(xlUp).Row
        wb.Sheets("Statement").Range("F6").Copy
        Sheet1.Range("A" & i & ":A" & k).PasteSpecial
        Application.CutCopyMode = False
    End If
    wb.Close
Next

Sheet1.Range("A1") = "All Invoices: " & Format(Date, "mmmm d, yyyy") & ", Week " & Format(Date, "ww")
'Align & Format Date text cell
Range("A1").RowHeight = 30
Range("A1").Font.Name = "Arial"
Range("A1").Font.Size = 16
Range("A1").IndentLevel = 1
Range("A1").VerticalAlignment = xlCenter
Range("A1").HorizontalAlignment = xlGeneral

MsgBox "Task Complete!"

With Application
 .EnableEvents = True
 .ScreenUpdating = True
 .Calculation = xlCalculationAutomatic
End With

End Sub

Thanks in advance...
Terry E
 
That looks like a temp file, is the code located in SCM Enterprises.xlsm?
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Well I got, finally, that there was a hidden file. I removed that and now get a new error.

Run-time error '9'
subscript out of range

I don't know what this one means.

Terry E
 
Upvote 0
It means that the sheet you are referencing does exist in that workbook
 
Upvote 0
Now I'm more confused. None of the programming calls sheets by name. It loops through all workbooks in the folder.

Also, am I missing something on getting the VB popup box to give me the debug option? I'm on Windows 10 and I'm using Subscription Office 365.

Terry E
 
Upvote 0
Which code are you talking about? The code in your OP, or the code in post#10?

Why you are not getting the standard "Debug" window, I have no idea, unless MS have changed it (which I doubt)
 
Upvote 0
The op (below). None of the programming calls anything by name except the sheet name "Statement" and all of my workbooks have been created from a template so they are ALL identical to each other. Like I said, up until last week this macro worked.

Code:
Sub DataExtract()

With Application
 .DisplayAlerts = False
 .ScreenUpdating = False
End With

Dim i As Long
Dim j As Long
Dim k As Long
Dim objFSO As Object
Dim objFolder As Object
Dim objFile  As Object
Dim wb As Workbook

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(ThisWorkbook.Path & "\Statements\")

For Each objFile In objFolder.Files
    If InStr(objFile, ".xls") Then
        Workbooks.Open (objFile)
    End If
    Set wb = ActiveWorkbook
    i = Sheet1.Cells(Rows.Count, "B").End(xlUp).Row + 1
    j = wb.Sheets("Statement").Cells(Rows.Count, "C").End(xlUp).Row
    If wb.Sheets("Statement").Range("C13") <> vbNullString Then
        wb.Sheets("Statement").Range("A13:F" & j).Copy
        Sheet1.Range("B" & i).PasteSpecial xlPasteValuesAndNumberFormats
        wb.Sheets("Statement").Range("I13:I" & j).Copy
        Sheet1.Range("H" & i).PasteSpecial xlPasteValuesAndNumberFormats
        k = Sheet1.Cells(Rows.Count, "B").End(xlUp).Row
        wb.Sheets("Statement").Range("F6").Copy
        Sheet1.Range("A" & i & ":A" & k).PasteSpecial
        Application.CutCopyMode = False
    End If
    wb.Close
Next

Sheet1.Range("A1") = "All Invoices: " & Format(Date, "mmmm d, yyyy") & ", Week " & Format(Date, "ww")
'Align & Format Date text cell
Range("A1").RowHeight = 30
Range("A1").Font.Name = "Arial"
Range("A1").Font.Size = 16
Range("A1").IndentLevel = 1
Range("A1").VerticalAlignment = xlCenter
Range("A1").HorizontalAlignment = xlGeneral

MsgBox "Task Complete!"

With Application
 .EnableEvents = True
 .ScreenUpdating = True
 .Calculation = xlCalculationAutomatic
End With

End Sub

Terry E
 
Upvote 0
Best guess is that either one of the workbooks does not have a sheet called "Statement" (may have been changed or deleted), or the workbook containing the code does not have a sheet with the codename "Sheet1"
 
Upvote 0
Eureka!

First off, thanks for all the help. The problem was another VERY hidden file. Unchecking the "show hidden..." did not show this file, it was "desktop.ini". Once I found it, and I only found it with the 2nd set of code I posted that had the ~?CMS file displayed. I missed this one in the list because it simply showed up in the "Ds" instead of at the bottom of the list like the ~?CMS file did.

The code is working now and I want to say a big thank you for taking the time to help.

Terry E
 
Upvote 0
Glad you sorted it & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,316
Members
452,634
Latest member
cpostell

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