1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| Sub CopieFichiers()
Dim I As Long, nbLignes As Long
Dim LigneSource As Long, ColonneSource As Long, LigneDest As Long
Dim Chemin As String, Fichier As String
On Error GoTo Erreur
'Le chemin doit finir pas un "\"
Chemin = Sheets("paramètre").Range("D5")
If Right(Chemin, 1) <> "\" Then Chemin = Chemin & "\"
nbLignes = Sheets("paramètre").Cells(Rows.Count, "D").End(xlUp).Row
For I = 10 To nbLignes
'lecture du nom du fichier
Fichier = Sheets("paramètre").Range("D" & I)
'Vérifie que le fichier existe
If Dir(Chemin & Fichier) <> "" Then 'existe
'ouvre le fichier
Workbooks.Open Chemin & Fichier
'détermine le nombre de lignes et colonnes à copier en tenant compte qu'il y a des entêtes en ligne 1
LigneSource = ActiveWorkbook.Sheets("Synthèse").Cells(Rows.Count, "A").End(xlUp).Row
ColonneSource = ActiveWorkbook.Sheets("Synthèse").Cells(1, Columns.Count).End(xlToLeft).Column
'détermine la 1ere ligne vide dans le fichier maître
LigneDest = ThisWorkbook.Sheets("Résultat").Cells(Rows.Count, "A").End(xlUp).Row
'copie les données telles quelles avec format, formules,...
ActiveWorkbook.Sheets("Synthèse").Range(Cells(2, 1), Cells(LigneSource, ColonneSource)).Copy _
ThisWorkbook.Sheets("Résultat").Range("A" & LigneDest)
'ferme le fichier qu'on vient de copier sans sauvegarde
ActiveWorkbook.Close False
Else
MsgBox "Le fichier " & Fichier & " n'existe pas dans le répertoire " & Chemin
End If
Next
MsgBox "Terminé"
Exit Sub
Erreur:
MsgBox Err.Number & vbCrLf & Err.Description
End Sub |
Partager