VBA: Erreur 1004 dans une boucle
bigpatou
-
gbinforme Messages postés 15481 Date d'inscription Statut Contributeur Dernière intervention -
gbinforme Messages postés 15481 Date d'inscription Statut Contributeur Dernière intervention -
Bonjour,
Je planche sur cette erreur depuis maintenant pas mal de temps sans grand succès:
J'ai des modifcations à effectuer sur des graphiques dans différentes feuilles excels placées dans le même dossier.
J'ai donc créé une boucle dans une boucle pour pouvoir gérer la modification de tous les graphs dans tous les fichiers.
A ma grande surpise, la boucle fonctionne très bien pour le premier fichier puis se bloque lors de sa deuxième itération en mode error 1004 disant que le "If ActiveChart.ChartTitle.Text <> "Net Revenue (k€)" Then" ne peut plus marcher...
Quelqu'un à une idée... s'il vous plaît?
Voici mon code
Private Sub Commande1_Click()
'
' travail_reloud Macro
' faire un travail reloud
'
Dim Chart As ChartObject
Dim xls As Excel.Application
Dim xlSheet As Excel.Worksheet
Dim xlBook As Excel.Workbook
Dim Repertoire, Chemin, Fichier, Cheminfin As String
Dim objShell As Shell
Dim objDossier, objFSO
Dim x
'on défini le répertoire de travail
Repertoire = CurrentProject.Path & "\Tblx de bord - CIPS 1 V6"
'Définition du prefixe du nom de fichier
Fichier = "Tblx de bord - CIPS 1 V6 -"
'Initialisation de l'énumération des fichiers
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDossier = objFSO.GetFolder(Repertoire)
For Each objFichier In objDossier.Files
Chemin = Repertoire & "\" & Fichier & Right(objFichier.Name, 9)
Set xls = CreateObject("Excel.Application")
Set xlBook = xls.Workbooks.Open(Chemin)
Set xlSheet = xlBook.Worksheets("Print CIPS")
For Each Chart In xlSheet.ChartObjects
Chart.Activate
If ActiveChart.ChartTitle.Text <> "Net Revenue (k€)" Then
ActiveChart.Axes(xlValue).MinimumScale = 0
ActiveChart.Axes(xlValue).MaximumScale = 1
ActiveChart.Axes(xlValue).MajorUnit = 0.1
End If
Next
ActiveWorkbook.Save
ActiveWorkbook.Close
Cheminfin = CurrentProject.Path & "\Tblx de bord - CIPS 1 V7" & "\Tblx de bord - CIPS 1 V7 -" & Right(objFichier.Name, 9)
objFSO.MoveFile Chemin, Cheminfin
Set xlSheet = Nothing
Set xlBook = Nothing
Set xls = Nothing
Set Chart = Nothing
Set ChartObjects = Nothing
Next
End Sub
Je planche sur cette erreur depuis maintenant pas mal de temps sans grand succès:
J'ai des modifcations à effectuer sur des graphiques dans différentes feuilles excels placées dans le même dossier.
J'ai donc créé une boucle dans une boucle pour pouvoir gérer la modification de tous les graphs dans tous les fichiers.
A ma grande surpise, la boucle fonctionne très bien pour le premier fichier puis se bloque lors de sa deuxième itération en mode error 1004 disant que le "If ActiveChart.ChartTitle.Text <> "Net Revenue (k€)" Then" ne peut plus marcher...
Quelqu'un à une idée... s'il vous plaît?
Voici mon code
Private Sub Commande1_Click()
'
' travail_reloud Macro
' faire un travail reloud
'
Dim Chart As ChartObject
Dim xls As Excel.Application
Dim xlSheet As Excel.Worksheet
Dim xlBook As Excel.Workbook
Dim Repertoire, Chemin, Fichier, Cheminfin As String
Dim objShell As Shell
Dim objDossier, objFSO
Dim x
'on défini le répertoire de travail
Repertoire = CurrentProject.Path & "\Tblx de bord - CIPS 1 V6"
'Définition du prefixe du nom de fichier
Fichier = "Tblx de bord - CIPS 1 V6 -"
'Initialisation de l'énumération des fichiers
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDossier = objFSO.GetFolder(Repertoire)
For Each objFichier In objDossier.Files
Chemin = Repertoire & "\" & Fichier & Right(objFichier.Name, 9)
Set xls = CreateObject("Excel.Application")
Set xlBook = xls.Workbooks.Open(Chemin)
Set xlSheet = xlBook.Worksheets("Print CIPS")
For Each Chart In xlSheet.ChartObjects
Chart.Activate
If ActiveChart.ChartTitle.Text <> "Net Revenue (k€)" Then
ActiveChart.Axes(xlValue).MinimumScale = 0
ActiveChart.Axes(xlValue).MaximumScale = 1
ActiveChart.Axes(xlValue).MajorUnit = 0.1
End If
Next
ActiveWorkbook.Save
ActiveWorkbook.Close
Cheminfin = CurrentProject.Path & "\Tblx de bord - CIPS 1 V7" & "\Tblx de bord - CIPS 1 V7 -" & Right(objFichier.Name, 9)
objFSO.MoveFile Chemin, Cheminfin
Set xlSheet = Nothing
Set xlBook = Nothing
Set xls = Nothing
Set Chart = Nothing
Set ChartObjects = Nothing
Next
End Sub
A voir également:
- VBA: Erreur 1004 dans une boucle
- Excel compter cellule couleur sans vba - Guide
- Incompatibilité de type vba ✓ - Forum VB / VBA
- Erreur 13 incompatibilité de type VBA excel ✓ - Forum Excel
- Find vba - Astuces et Solutions
- Vba range avec variable ✓ - Forum VB / VBA