Continuo a ricevere questo errore di runtime 1004. Ho ridotto la mia programmazione in qualche modo quindi non è così Programception. Penso che possa avere a che fare con l'utilizzo di Excel 2010 per salvare i file .xls. Non sono sicuro.Macro che esegue una macro che apre i file e li salva come valore - Errore di runtime 1004
- Quando Auto_Root.xls apre Corre Auto_apri Sub(), che si apre si apre Panel.xls
- Panel e corre aggiornamento Sub(), che in sequenza si apre 7 file in diverse directory tutti chiamati Auto_Update.xls
- Auto_Update.xsl apre ed esegue Sub Flat che aprono ciascuno un numero di file in sequenza e ne salva una copia flat in un'altra directory .
Ho aperto ciascuno dei 7 file Auto_Update.xls e li ho eseguiti indipendentemente e corrono senza errori. Quando li eseguo tutti da Auto_Root ottengo un errore di runtime 1004. E CurrentWB.Save è evidenziato su uno dei file. Ho persino sostituito CurrentWB.Save come nome file CurrentWB.SaveAs: = TargetFile, FileFormat: = xlNormal e ho ricevuto lo stesso errore di runtime.
In allegato è il codice che ho.
AutoRoot.xls! Auto 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! Aggiornare
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! Piatto
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
Quello che ho fatto finora.
- Ogni singolo file di AutoUpdate funziona quando è acceso.
- Se Application.Run "Auto_Update.xls! Flat" viene rimosso da Panel.xls! Aggiorna apre e chiude tutti i file AutoUpdate.xls senza errori.
- Se collego Panel.xls! Update a solo 3 dei 7 file di AutoUpdate .... any 3. Funziona senza errori.
io proprio non riesco a farlo funzionare tutto senza dire 7 Errore di runtime 1004.
ho trovato un lavoro intorno Microsoft codice. Non so come implementarlo però.
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
Quale riga genera l'errore? –
Avete definito correttamente gli intervalli NAMED per 'File' e 'Path' e denominati ONE cell come 'Sys.Path' e 'Report.Path' e 'SubFolder'? Inoltre, quali valori hai in "Sys.Path" (e altre informazioni ??) –
la cartella di lavoro ha l'attributo di sola lettura impostato? – Sorceri