Erreur 1004 sur formule
Bonjour, j'ai réalisé une formule qui fonctionne de manière aléatoire (des fois oui, des fois non) avec une erreur 1004. Cette formule me permet de transférer des graphiques excel sur un document word avec des signets. Pour chaque entreprise, 6 graphiques (nommés "graphique 2", "graphique 3") correspondent . Voici la formule :
Sub graph()
Dim EmpDoc
'récupère l'emplacement et le nom de fichier dans la variable EmpDoc
EmpDoc = Sheets("Parametreseditions").Range("B1").Value & "\" & Sheets("Parametreseditions").Range("B2")
MsgBox (EmpDoc)
'ouvre le document word
Set WordApp = CreateObject("word.Application")
WordApp.Visible = True
Set WordDoc = WordApp.Documents.Open(EmpDoc)
'active la feuille "parametreseditions"
Sheets("parametreseditions").Activate
'Si la cellule A6 de la page "parametreseditions" est "Entreprise 1"
If Range("A6").Value = "Entreprise 1" Then
'active la feuille "Graphiques Détail"
Sheets("Graphiques Détail").Activate
'récupère le graphique 2 et le colle sur le signet 01 de fichier word
ActiveSheet.ChartObjects("Graphique 2").Copy
WordDoc.Bookmarks("Signet01").Range.PasteAndFormat (13)
'//
ActiveSheet.ChartObjects("Graphique 11").Copy
WordDoc.Bookmarks("Signet02").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 20").Copy
WordDoc.Bookmarks("Signet03").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 29").Copy
WordDoc.Bookmarks("Signet04").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 38").Copy
WordDoc.Bookmarks("Signet05").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 56").Copy
WordDoc.Bookmarks("Signet06").Range.PasteAndFormat (13)
End If
If Range("A6").Value = "Entreprise 2" Then
Sheets("Graphiques Détail").Activate
ActiveSheet.ChartObjects("Graphique 3").Copy
WordDoc.Bookmarks("Signet01").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 12").Copy
WordDoc.Bookmarks("Signet02").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 21").Copy
WordDoc.Bookmarks("Signet03").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 30").Copy
WordDoc.Bookmarks("Signet04").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 39").Copy
WordDoc.Bookmarks("Signet05").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 57").Copy
WordDoc.Bookmarks("Signet06").Range.PasteAndFormat (13)
End If
If Range("A6").Value = "Entreprise 3" Then
Sheets("Graphiques Détail").Activate
ActiveSheet.ChartObjects("Graphique 4").Copy
WordDoc.Bookmarks("Signet01").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 13").Copy
WordDoc.Bookmarks("Signet02").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 22").Copy
WordDoc.Bookmarks("Signet03").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 31").Copy
WordDoc.Bookmarks("Signet04").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 40").Copy
WordDoc.Bookmarks("Signet05").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 58").Copy
WordDoc.Bookmarks("Signet06").Range.PasteAndFormat (13)
End If
If Range("A6").Value = "Entreprise 4" Then
Sheets("Graphiques Détail").Activate
ActiveSheet.ChartObjects("Graphique 5").Copy
WordDoc.Bookmarks("Signet01").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 14").Copy
WordDoc.Bookmarks("Signet02").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 23").Copy
WordDoc.Bookmarks("Signet03").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 32").Copy
WordDoc.Bookmarks("Signet04").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 41").Copy
WordDoc.Bookmarks("Signet05").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 59").Copy
WordDoc.Bookmarks("Signet06").Range.PasteAndFormat (13)
End If
If Range("A6").Value = "Entreprise 5" Then
Sheets("Graphiques Détail").Activate
ActiveSheet.ChartObjects("Graphique 6").Copy
WordDoc.Bookmarks("Signet01").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 15").Copy
WordDoc.Bookmarks("Signet02").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 24").Copy
WordDoc.Bookmarks("Signet03").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 33").Copy
WordDoc.Bookmarks("Signet04").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 42").Copy
WordDoc.Bookmarks("Signet05").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 60").Copy
WordDoc.Bookmarks("Signet06").Range.PasteAndFormat (13)
End If
If Range("A6").Value = "Entreprise 6" Then
Sheets("Graphiques Détail").Activate
ActiveSheet.ChartObjects("Graphique 7").Copy
WordDoc.Bookmarks("Signet01").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 16").Copy
WordDoc.Bookmarks("Signet02").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 25").Copy
WordDoc.Bookmarks("Signet03").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 34").Copy
WordDoc.Bookmarks("Signet04").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 43").Copy
WordDoc.Bookmarks("Signet05").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 61").Copy
WordDoc.Bookmarks("Signet06").Range.PasteAndFormat (13)
End If
If Range("A6").Value = "Entreprise 7" Then
Sheets("Graphiques Détail").Activate
ActiveSheet.ChartObjects("Graphique 8").Copy
WordDoc.Bookmarks("Signet01").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 17").Copy
WordDoc.Bookmarks("Signet02").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 26").Copy
WordDoc.Bookmarks("Signet03").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 35").Copy
WordDoc.Bookmarks("Signet04").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 44").Copy
WordDoc.Bookmarks("Signet05").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 62").Copy
WordDoc.Bookmarks("Signet06").Range.PasteAndFormat (13)
End If
If Range("A6").Value = "Entreprise 8" Then
Sheets("Graphiques Détail").Activate
ActiveSheet.ChartObjects("Graphique 9").Copy
WordDoc.Bookmarks("Signet01").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 18").Copy
WordDoc.Bookmarks("Signet02").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 27").Copy
WordDoc.Bookmarks("Signet03").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 36").Copy
WordDoc.Bookmarks("Signet04").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 45").Copy
WordDoc.Bookmarks("Signet05").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 63").Copy
WordDoc.Bookmarks("Signet06").Range.PasteAndFormat (13)
End If
If Range("A6").Value = "Entreprise 9" Then
Sheets("Graphiques Détail").Activate
ActiveSheet.ChartObjects("Graphique 10").Copy
WordDoc.Bookmarks("Signet01").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 19").Copy
WordDoc.Bookmarks("Signet02").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 28").Copy
WordDoc.Bookmarks("Signet03").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 37").Copy
WordDoc.Bookmarks("Signet04").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 46").Copy
WordDoc.Bookmarks("Signet05").Range.PasteAndFormat (13)
ActiveSheet.ChartObjects("Graphique 64").Copy
WordDoc.Bookmarks("Signet06").Range.PasteAndFormat (13)
End If
End SubEdit modo : merci de mettre le code entre balises SVP
Il existe sûrement un moyen de la simplifier pour la rendre optimale. Merci d'avance pour vos retours
Bonjour JoHM
la réponse est peut-être là :
Pour simplifier la procédure il faudrait dimensionner une variable tableau contenant les noms des entreprises et les N° de graphiques, et faire deux boucles imbriquées, une sur les noms et l'autre sur les N°
Cdt
Bonjour Toukoul, merci de ton intervention. J'ai regardé les méthodes pour résoudre le pb via ton lien et je ne peux pas faire ça.. Pourrais-tu m'en dire plus et m'expliquer la solution de simplification que tu proposes stp?
JoHM
Quelque chose qui ressemblerait à ça:
Dim x, y As Integer
Dim TableatChoix(9, 6) As String
'Tableau pour 9 entreprises et 6 graphiques chacune
For x = 0 To 8 'entreprises
For y = 0 To 6 ' graphiques
' Tableau(x,y)= range Cellules ou sont mis les noms d'entreprises et les NOMS des graphiques associés
Next y
Next x
' Tableau (0,0)= nom première entreprise
' Tableau (1,0)= nom deuxième entreprise
' Tableau (0,1)= nom premier graphique de la première entreprise
' Tableau (3,2)= nom Deuxième graphique de la quatrième entreprise
For x = 0 To 8 'entreprises
If Range("A6").Value = tableau(x, 0) Then
For y = 1 To 6 ' graphiques
Sheets("Graphiques Détail").Activate
ActiveSheet.ChartObjects(TableatChoix(x, y)).Copy
WordDoc.Bookmarks("Signet0" & y).Range.PasteAndFormat (13)
End If
Next y
Next x
Je comprends un peu le principe mais je ne vois pas comment appliquer ta formule à ma situation.. Je suis novice en la matière, si tu as peut-être un peu plus de temps pour m'expliquer la logique, notamment sur les tableaux. Il faut que je crée les tableaux sur excel?
Malheureusement, je ne peux pas partager mon fichier excel, c'est un dossier professionnel. En espérant tout de même que tu puisses parvenir à m'expliquer ta logique:)
N'oubliez pas que cela ne fait que réduire la longueur du code, réglez d'abord le problème de l'erreur 1004
travaillez vous sur mac ou pc?
merci
Je travail sur PC. Pour exposer mon problème, mon bouton d'exécution de la formule se trouve sur une feuille de mon fichier excel. La feuille "graphiques détail" est une autre feuille. Si j'ouvre mon doc et que je cherche à faire tourner la formule, j'aurai l'erreur 1004. Mais, si je vais avant sur la feuille "graphiques détail" et que je clique sur chaque graphique (pour chaque entreprise) , c'est comme si le fichier excel reconnaissait enfin les graphiques, et ma formule s'applique parfaitement (les graphiques choisis vont au bon emplacement et s'affichent tous sur mon doc word) :
EX : pour l'entreprise 1, je dois exporter le graphique 2, 11, 20, 29, 38 et 56 et les graphiques de chaque entreprise suivent la ligne (si mon entreprise est en A2, les graphiques seront en cellule B2, C2, D2, E2, F2 et G2. Il faut simplement que je clique sur chacun d'entre eux pour que, lorsque j'appuie sur mon bouton, tout s'affiche bien : pour mon entreprise 1 au niveau du signet01 se trouve bien le graphique 2, au niveau du signet02 se trouve bien le graphique 11, etc. Le problème est qu'il est handicapant de devoir sans cesse cliquer sur les graphiques correspondant, on perd l'intérêt de créer une macro qui automatise tout le travail.
J'espère avoir réussi à être clair dans mes propos
Merci pour l'intérêt que vous portez à mon pb en tout cas!
- Messages
- 3'678
- Excel
- 365, 2019
- Inscrit
- 11/04/2020
- Emploi
- Formateur bureautique, dvpt de fichiers
Ce n'est pas super clean je pense, mais en faisant
Sheets("Graphiques Détail").Activate
ActiveSheet.ChartObjects("Graphique 10").select
Selection.Copy
WordDoc.Bookmarks("Signet01").Range.PasteAndFormat (13)en demandant donc à l'ordi de sélectionner le graphique avant pour toi, ça doit pouvoir passer.
Je vais essayer ta méthode et te dire si ça fonctionne.J'ai trouvé une méthode alternative. En fait, la formule ne prend en compte que les graphiques visibles à l'écran (or en zoom 100% on ne voit pas tous les graphiques), j'ai donc "dézoomé la feuille" à 20% et là on voit bien les 6 graphiques des 9 entités. Et en effet, lorsque je relance mon fichier excel, que je vais directement sur mon bouton, que je prends n'importe qu'elle entité, les graphiques s'affichent proprement. De ce que je comprends, l'erreur vient d'une impossibilité de voir l'ensemble des graphiques. La formule bloque car elle n'identifie pas les derniers graphiques de la ligne.
Ta méthode fonctionne, merci bcp pour ton aide. Bonne continuation!