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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| Option Explicit
Dim wbk As Workbook, wbksource As Workbook, Fichierexistant As Range, Plagederecherche As Range
Dim Chemin As String, Nomfichier As String, Fichiersource As String, NomOnglet As String, xsrefn, xsdate
Dim xdlgn As Long, xdlgnsource As Long, xnblgnsource As Long, i As Long, j As Long, xlgn As Integer, xcol As Integer
Private Sub CommandButton1_Click()
' Lancer la consolidation
' Tous les fichiers doivent se trouver dans le même répertoire - extension xlsm -
' ainsi que le fichier RECAPITULATIF.xlsm
Application.ScreenUpdating = False
'Efface les données
Range("a5:L5000").Select
Selection.Value = ""
Application.DisplayAlerts = False
Chemin = ThisWorkbook.Path & "\"
Set wbk = ActiveWorkbook
Set Plagederecherche = wbk.Sheets(1).Columns(1)
Nomfichier = Dir(Chemin & "\NDF*.xlsm")
Do While Nomfichier <> ""
' Méthode Find
Set Fichierexistant = Plagederecherche.Cells.Find(What:=Nomfichier, LookAt:=xlWhole)
If Fichierexistant Is Nothing Then
Set wbksource = Nothing
Fichiersource = Chemin & Nomfichier
Set wbksource = Workbooks.Open(Filename:=Fichiersource)
xdlgnsource = wbksource.Sheets("JANVIER").Range("A28").End(xlUp).Row: xnblgnsource = xdlgnsource - 2
' NomOnglet = Workbooks.Sheets(1).Name
' Copie les données dans le fichier RECAPITULATIF
With wbk.Sheets(1)
xdlgn = .Range("A" & Rows.Count).End(xlUp).Row + 1
xlgn = 5: xcol = 1: j = xdlgn
For i = xdlgn To xdlgn + xnblgnsource
wbk.Sheets(1).Cells(j, 1) = Nomfichier
wbk.Sheets(1).Cells(j, 2) = NomOnglet
wbk.Sheets(1).Cells(j, 3).Value = wbksource.Sheets("JANVIER").Cells(xlgn, 1).Value
wbk.Sheets(1).Cells(j, 4).Value = wbksource.Sheets("JANVIER").Cells(xlgn, 2).Value
wbk.Sheets(1).Cells(j, 5).Value = wbksource.Sheets("JANVIER").Cells(xlgn, 3).Value
wbk.Sheets(1).Cells(j, 6).Value = wbksource.Sheets("JANVIER").Cells(xlgn, 4).Value
wbk.Sheets(1).Cells(j, 7).Value = wbksource.Sheets("JANVIER").Cells(xlgn, 5).Value
wbk.Sheets(1).Cells(j, 8).Value = wbksource.Sheets("JANVIER").Cells(xlgn, 6).Value
wbk.Sheets(1).Cells(j, 9).Value = wbksource.Sheets("JANVIER").Cells(xlgn, 7).Value
wbk.Sheets(1).Cells(j, 10).Value = wbksource.Sheets("JANVIER").Cells(xlgn, 8).Value
wbk.Sheets(1).Cells(j, 11).Value = wbksource.Sheets("JANVIER").Cells(xlgn, 9).Value
wbk.Sheets(1).Cells(j, 12).Value = wbksource.Sheets("JANVIER").Cells(xlgn, 10).Value
j = j + 1
xlgn = xlgn + 1
Next i
End With
wbksource.Close
End If
Nomfichier = Dir
Loop
Set wbk = Nothing
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "Traitement terminé."
End Sub |
Partager