Erreur inattenue - Echec de la copie
Bonjour à tous,
Je vous souhaite tout d'abord une très bonne année !
J'ai besoin d'un coup de pouce. J'ai créé une macro en 2 parties:
-La première partie va chercher 2 exports excel via des requêtes puis les fusionne (via une 3ème requete) pour obtenir un tableau complet
-La deuxième partie supprime des colonnes, fait de la mise en forme et enregistre le tout dans un nouveau fichier excel.
Lorsque que j’exécute ces 2 macros séparément, l'une après l'autre, tout se passe nickel.
J'ai ensuite rassemblé les 2 macros dans une seule macro -> ça s'exécute mais j'obtiens un message d'erreur à la fin "Erreur inattendue" - "Echec de la copie".
C'est bizarre car il ne me semble pas que je demande de copie de données ?!
Même message d'erreur quand je crée une nouvelle macro qui appelle les 2 précédentes macros l'une après l'autre.
J'ai essayé d'ajouter une temporisation entre l'exécution des 2 macros : toujours au même point !
J'ai redémarré le PC, toujours pareil
J'ai tenté de vider le presse-papier excel : toujours rien.
Voilà donc la fameuse macro en question (j'ai enregistré puis modifié donc le code est surement pas vraiment optimisé...) : j'ai retiré quelques lignes au niveau des requêtes pour essayer de clarifier un peu....
Sub REQ_MEF_PROJECT()
'PARTIE 1 : REQUETE POUR FUSION DES 2 TABLEAUX BUDG et OPE
'Requete pour importer le premier tableau BUDG
ActiveWorkbook.Queries.Add Name:="BUDG pour PROJECT", Formula:= _
"let" & Chr(10) & " Source = Excel.Workbook(File.Contents(""C:\Users\FHLAMBY\Tests PROJECT\EXPORT\BUDG.xlsx""), null, true)," & Chr(10) & " #""Export orchestra 1_Sheet"" = Source{[Item=""Export orchestra 1"",Kind=""Sheet""]}[Data]," & Chr(10) & " #""En-têtes promus"" = Table.PromoteHeaders(#""Export orchestra 1_Sheet"", [PromoteAllScalars=true])," & Chr(10) & " #""Type modifié"" = Table.Transfo" & _
"rmColumnTypes(#""En-têtes promus"",{{""|N° LIGNE________|"", type text}, {""|CODE OPÉRATION|"", type text}, {""|CODE UE|"", type text},
ETC avec toutes les autres colonnes})," & Chr(10) & "
#""Colonnes supprimées"" = Table.RemoveColumns(#""Type modifié"",{""|DESCRIPTION__________________|"",
ETC avec toutes les colonnes supprimées)" & Chr(10) & "in" & Chr(10) & " #""Colonnes supprimées"""
Sheets.Add After:=ActiveSheet
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""BUDG pour PROJECT"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [BUDG pour PROJECT]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = False
.ListObject.DisplayName = "BUDG_pour_PROJECT"
.Refresh BackgroundQuery:=False
End With
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
'Requete pour importer le 2ème tableau OPE
ActiveWorkbook.Queries.Add Name:="OPE pour PROJECT", Formula:= _
"let" & Chr(10) & " Source = Excel.Workbook(File.Contents(""C:\Users\G42072\Documents\ELODIE\Tests PROJECT\EXPORT\OPE.xlsx""), null, true)," & Chr(10) & " #""Export orchestra 1_Sheet"" = Source{[Item=""Export orchestra 1"",Kind=""Sheet""]}[Data]," & Chr(10) & " #""En-têtes promus"" = Table.PromoteHeaders(#""Export orchestra 1_Sheet"", [PromoteAllScalars=true])," & Chr(10) & " #""Type modifié"" = Table.Transfor" & _
"mColumnTypes(#""En-têtes promus"",{{""|No OPÉRATION___|"", type text},
ETC avec toutes les autres colonnes)
," & Chr(10) & " #""Type modifié1"" = Table.TransformColumnTypes(#""Colonnes supprimées"",{{""Début réf."", type date}, {""Fin réf."", type date}, {""Durée réf."", type date}, {""Début cible"", type date}, {""Fin cible"", type date}, {""Durée cible"", type date}})" & Chr(10) & "in" & Chr(10) & " #""Type mod" & _"ifié1"""
Sheets.Add After:=ActiveSheet
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""OPE pour PROJECT"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [OPE pour PROJECT]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = False
.ListObject.DisplayName = "OPE_pour_PROJECT"
.Refresh BackgroundQuery:=False
End With
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
'Requete FUSION des 2 tableau en utilisant la colonne "Code opération" comme pivot et en ne gardant que quelques colonnes de OPE qui m'intéressent
ActiveWorkbook.Queries.Add Name:="FUSION pour PROJECT", Formula:= _
"let" & Chr(10) & " Source = Table.NestedJoin(#""BUDG pour PROJECT"",{""|CODE OPÉRATION|""},#""OPE pour PROJECT"",{""|No OPÉRATION___|""},""OPE pour PROJECT"",JoinKind.LeftOuter)," & Chr(10) & " #""OPE pour PROJECT développé"" = Table.ExpandTableColumn(Source, ""OPE pour PROJECT"", {""|No OPÉRATION___|"", ""Début réf."", ""Fin réf."", ""Durée réf."", ""Début cible"", ""Fin cible"", ""Duré" & _
"e cible""}, {""OPE pour PROJECT.|No OPÉRATION___|"", ""OPE pour PROJECT.Début réf."", ""OPE pour PROJECT.Fin réf."", ""OPE pour PROJECT.Durée réf."", ""OPE pour PROJECT.Début cible"", ""OPE pour PROJECT.Fin cible"", ""OPE pour PROJECT.Durée cible""})" & Chr(10) & "in" & Chr(10) & " #""OPE pour PROJECT développé"""
Sheets.Add After:=ActiveSheet
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""FUSION pour PROJECT"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [FUSION pour PROJECT]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = False
.ListObject.DisplayName = "FUSION_pour_PROJECT"
.Refresh BackgroundQuery:=False
End With
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
'Renommer feuille en cours en "INPUT_PROJ" + supprimer les feuilles créées devenues inutiles
Sheets("Feuil4").Select
Sheets("Feuil4").Name = "INPUT_PROJ"
Application.DisplayAlerts = False
Sheets(Array("Feuil1", "Feuil2", "Feuil3")).Select
'Sheets("Feuil1").Activate
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
'Ajout de cette instruction pour déselectionner tout le tableau mais ça ne change rien
Range("FUSION_pour_PROJECT[[#Headers],[|N° LIGNE________|]]").Select
' PARTIE 2 : MISE EN FORME DU TABLEAU
'Je supprime les colonnes qui ne m'intéressent pas
Columns("A:A").Select
Selection.Delete Shift:=xlToLeft
Columns("B:B").Select
Selection.Delete Shift:=xlToLeft
Columns("C:E").Select
Selection.Delete Shift:=xlToLeft
Columns("E:H").Select
Selection.Delete Shift:=xlToLeft
Columns("G:AT").Select
Selection.Delete Shift:=xlToLeft
Columns("I:I").Select
Selection.Delete Shift:=xlToLeft
Columns("K:K").Select
Selection.Delete Shift:=xlToLeft
'Je filtre et retire des infos qui ne m'intéressent pas
ActiveSheet.ListObjects("FUSION_pour_PROJECT").Range.AutoFilter Field:=6, _
Criteria1:=Array("ETRVXOENED", "ETRVXPENED", "INCONNU", "="), Operator:= _
xlFilterValues
Rows("2:5000").Select
Selection.Delete Shift:=xlUp
ActiveSheet.ListObjects("FUSION_pour_PROJECT").Range.AutoFilter Field:=6
'Je supprime la colonne F qui ne me sert plus
Columns("F:F").Select
Selection.Delete Shift:=xlToLeft
'Mise en forme en-tête couleurs
Rows("1:1").Select
Selection.RowHeight = 24
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Range("A1:I1").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 1399038
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Rows("1:1").Select
Range("B1").Activate
Selection.Font.Bold = True
'Renommage intitulés de colonnes
Range("A1").Select
ActiveCell.FormulaR1C1 = "CODE"
Range("B1").Select
ActiveCell.FormulaR1C1 = "SITE"
Range("C1").Select
ActiveCell.FormulaR1C1 = "INTITULE"
Range("D1").Select
ActiveCell.FormulaR1C1 = "TYPE"
Range("E1").Select
ActiveCell.FormulaR1C1 = "PAYEUR"
Range("F1").Select
ActiveCell.FormulaR1C1 = "DEBUT_REF"
Range("G1").Select
ActiveCell.FormulaR1C1 = "FIN_REF"
Range("H1").Select
ActiveCell.FormulaR1C1 = "DEBUT_CIBLE"
Range("I1").Select
ActiveCell.FormulaR1C1 = "FIN_CIBLE"
'Redimensionnement largeur colonnes
Columns("A:A").Select
Selection.ColumnWidth = 20
Columns("B:B").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 55
Columns("D:D").Select
Selection.ColumnWidth = 5
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("F:F").Select
Selection.ColumnWidth = 11
Columns("G:G").Select
Selection.ColumnWidth = 11
Columns("H:H").Select
Selection.ColumnWidth = 11
Columns("I:I").Select
Selection.ColumnWidth = 11
'On retire les doublons en code opération + Type
Range("A2").Select
ActiveSheet.Range("FUSION_pour_PROJECT[#All]").RemoveDuplicates Columns:= _
Array(1, 4), Header:=xlYes
'Format date pour les dates
Columns("F:I").Select
Selection.NumberFormat = "m/d/yyyy"
'Figer les volets
Rows("2:2").Select
Range("B2").Activate
ActiveWindow.FreezePanes = True
'Enregistrement du fichier "Input_PROJ"
ChDir "C:\Users\FHLAMBY\Tests PROJECT"
ActiveWorkbook.SaveAs Filename:= _
"C:\Users\FHLAMBY\Tests PROJECT\INPUT_PROJ.xlsx", FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
End SubBref, voilà, je suis à court d'idées. La macro a l'air de tourner mais j'ai toujours cette erreur à la fin quand je l'enchaine, et ça me rend dingue...
Merci par avance pour vos suggestions...
Fhlamby