I keep getting this runtime error 1004. I shortened my program a bit so that it wasn’t such software. I think this could be due to using Excel 2010 to save .xls files. Not sure.
- When Auto_Root.xls opens, it launches Sub auto_open (), which opens Panel.xls
- The panel opens and launches Sub Update (), which sequentially opens 7 files in different directories, all called Auto_Update.xls
- Auto_Update.xsl opens and launches Sub Flat, each of which opens several files sequentially and saves a flat copy of itself to another directory.
I opened each of the 7 Auto_Update.xls files and ran them myself, and they started without errors. When I run them all from Auto_Root, I get a 1004 runtime error. And CurrentWB.Save stands out in one of the files. I even replaced CurrentWB.Save as CurrentWB.SaveAs Filename: = TargetFile, FileFormat: = xlNormal and got the same runtime error.
The attached code that I have.
AutoRoot.xls! Automatic update
Sub auto_open() Application.CutCopyMode = False Dim PanelFilePath As String Dim PanelFileName As String Dim PanelLocation As String Dim PanelWB As Workbook PanelFilePath = "D:\umc\UMC Production Files\Automation Files\" PanelFileName = "Panel.xls" PanelLocation = PanelFilePath & Dir$(PanelFilePath & PanelFileName) Set PanelWB = Workbooks.Open(Filename:=PanelLocation, UpdateLinks:=3) PanelWB.RunAutoMacros Which:=xlAutoOpen Application.Run "Panel.xls!Update" PanelWB.Close Call Shell("D:\umc\UMC Production Files\Automation Files\Auto.bat", vbNormalFocus) Application.Quit End Sub
Panel.xls! Update
Sub Update() Dim RowNumber As Long Dim AutoUpdateTargetFile As String Dim AutoUpdateWB As Workbook For RowNumber = 1 To (Range("AutoUpdate.File").Rows.Count - 1) If (Range("AutoUpdate.File").Rows(RowNumber) <> "") Then AutoUpdateTargetFile = Range("Sys.Path") & Range("Client.Path").Rows(RowNumber) & Range("AutoUpdate.Path ").Rows(RowNumber) & Range("AutoUpdate.File").Rows(RowNumber) Set AutoUpdateWB = Workbooks.Open(Filename:=AutoUpdateTargetFile, UpdateLinks:=3) AutoUpdateWB.RunAutoMacros Which:=xlAutoOpen Application.Run "Auto_Update.xls!Flat" AutoUpdateWB.Close End If Next RowNumber End Sub
AutoUpdate.xls! Flat
Sub Flat() Dim RowNumber As Long 'Long Stores Variable Dim SheetNumber As Long Dim TargetFile As String 'String Stores File Path Dim BackupFile As String Dim CurrentWB As Workbook 'Workbook Stores Workbook For RowNumber = 1 To (Range("File").Rows.Count - 1) 'Loops through each file in the list and assigns a workbook variable. If (Range("File").Rows(RowNumber) <> "") Then TargetFile = Range("Sys.Path") & Range("Path").Rows(RowNumber) & Range("File").Rows(RowNumber) 'Target File Path BackupFile = Range("Report.Path") & Range("Path").Rows(RowNumber) & Range("SubFolder") & Range("File").Rows(RowNumber) 'Backup File Path Set CurrentWB = Workbooks.Open(Filename:=TargetFile, UpdateLinks:=3) 'Sets CurrentWB = to that long name. This becomes the name of the workbook. CurrentWB.RunAutoMacros Which:=xlAutoOpen 'Enables Macros in Workbook CurrentWB.SaveAs Filename:=TargetFile, FileFormat:=56 For SheetNumber = 1 To Sheets.Count 'Counts Worksheets in Workbook Sheets(SheetNumber).Select 'Selects All Worksheets in Workbook If (Sheets(SheetNumber).Name <> "What If") Then Sheets(SheetNumber).Unprotect ("UMC626") 'Unprotects Workbook Cells.Select 'Selects Data in Workbook Range("B2").Activate With Sheets(SheetNumber).UsedRange .Value = .Value End With Sheets(SheetNumber).Protect Password:="UMC626", DrawingObjects:=True, Contents:=True, Scenarios:=True 'Protects Workbook End If Next SheetNumber 'Runs Through Iteration Sheets(1).Select Range("A1").Select 'Saves each workbook at the top of the page CurrentWB.SaveAs Filename:=BackupFile, FileFormat:=56, Password:="", WriteResPassword:="", _ ReadOnlyRecommended:=False, CreateBackup:=False 'Saves Workbook in Flatten File Location CurrentWB.Close 'Closes Workbook End If 'Ends Loop Next RowNumber 'Selects Another Account End Sub
What I have done so far.
- Each individual AutoUpdate file works at startup.
- If Application.Run "Auto_Update.xls! Flat" is removed from Panel.xls! The update opens and closes all AutoUpdate.xls files without errors.
- If I Link Panel.xls! I update only 3 of 7 AutoUpdate files .... any 3. It works without errors.
I just can't get it to run all 7 without saying Runtime Error 1004.
I found that microsoft is working on code. Not sure how to implement it.
Sub CopySheetTest() Dim iTemp As Integer Dim oBook As Workbook Dim iCounter As Integer ' Create a new blank workbook: iTemp = Application.SheetsInNewWorkbook Application.SheetsInNewWorkbook = 1 Set oBook = Application.Workbooks.Add Application.SheetsInNewWorkbook = iTemp ' Add a defined name to the workbook ' that RefersTo a range: oBook.Names.Add Name:="tempRange", _ RefersTo:="=Sheet1!$A$1" ' Save the workbook: oBook.SaveAs "c:\test2.xls" ' Copy the sheet in a loop. Eventually, ' you get error 1004: Copy Method of ' Worksheet class failed. For iCounter = 1 To 275 oBook.Worksheets(1).Copy After:=oBook.Worksheets(1) 'Uncomment this code for the workaround: 'Save, close, and reopen after every 100 iterations: If iCounter Mod 100 = 0 Then oBook.Close SaveChanges:=True Set oBook = Nothing Set oBook = Application.Workbooks.Open("c:\test2.xls") End If Next End Sub
http://support.microsoft.com/kb/210684/en-us