Bonjour,
Je me permets de poser ce problème car je n'arrive pas à trouver la solution avec mes connaissances basiques en VBA.
En gros, je souhaite compiler plusieurs fichiers excel dans un onglet de synthèse contenant la même forme.
J'ai récupérer ce code que j'ai essayé d'adapter mais il ne compile pas toutes les lignes.
Je dois copier les données à partir de la ligne 3 (car il a 2 ligne d'en-tète), et les coller à la même ligne (car même structure). le problème est que quand je défini la plage A4:AM, il me prends que la ligne 3 et 4. et je met A10:AM, il me récupere 8 lignes, etc...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
65
66
67 Sub MergeSelectedWorkbooks() Dim SummarySheet As Worksheet Dim FolderPath As String Dim SelectedFiles() As Variant Dim NRow As Long Dim FileName As String Dim NFile As Long Dim WorkBk As Workbook Dim SourceRange As range Dim DestRange As range Dim LastRow As Long ' Create a new workbook and set a variable to the first sheet. Set SummarySheet = ThisWorkbook.Sheets("Accueil") ' Modify this folder path to point to the files you want to use. FolderPath = "C:\Users\Yettou\Desktop\Test" ' Set the current directory to the the folder path. ChDrive FolderPath ChDir FolderPath ' Open the file dialog box and filter on Excel files, allowing multiple files ' to be selected. SelectedFiles = Application.GetOpenFilename( _ filefilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True) ' NRow keeps track of where to insert new rows in the destination workbook. NRow = 3 ' Loop through the list of returned file names For NFile = LBound(SelectedFiles) To UBound(SelectedFiles) ' Set FileName to be the current workbook file name to open. FileName = SelectedFiles(NFile) ' Open the current workbook. Set WorkBk = Workbooks.Open(FileName) ' Set the cell in column A to be the file name. LastRow = WorkBk.Worksheets(1).Cells.Find(What:="*", _ After:=WorkBk.Worksheets(1).Cells.range("A4"), _ SearchDirection:=xlPrevious, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows).Row Set SourceRange = WorkBk.Worksheets(1).range("A4:AM" & LastRow) ' Set the destination range to start at column A and be the same size as the source range. Set DestRange = SummarySheet.range("A" & NRow) Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _ SourceRange.Columns.Count) ' Copy over the values from the source to the destination. DestRange.Value = SourceRange.Value ' Increase NRow so that we know where to copy data next. NRow = NRow + DestRange.Rows.Count ' Close the source workbook without saving changes. WorkBk.Close savechanges:=False Next NFile ' Call AutoFit on the destination sheet so that all data is readable. SummarySheet.Columns.AutoFit End Sub
Avez vous une idée ?
Merci d'avance
Partager