Bonjour,
Je suis debutant en VBA et j'aimerais coder une macro pour un projet qui necessite une action repetitive.
Situation: Dans un document, j'ai plusieur fichier excel de performance de societes par annees. (Fichier 1= 2000 a 2004, F2= 2004-8, F3= 2009-12, F4=2013-15)
Objectif: Regrouper tous ces fichiers en un seul en copiant toutes ces colones a la suite.
Voila ce que j'ai pour le moment reussi a faire, mais mon module affiche constament des erreurs
Est ce aue auelau'un pourrait m'aider a corriger ce morceau de code stp?
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 Sub MergeAllWorkbooks() Dim SummarySheet As Worksheet Dim FolderPath As String Dim NRow As Long Dim Lc As Long Dim FileName As String Dim WorkBk As Workbook Dim SourceRange As Range Dim DestRange As Range Dim Lastcol As Range ' Create a new workbook and set a variable to the first sheet. Set SummarySheet = Workbooks.Add(xlWBATWorksheet).Worksheets(1) ' Modify this folder path to point to the files you want to use. FolderPath = "C:\Users\abcdefghij" ' NRow keeps track of where to insert new rows in the destination workbook. NRow = 1 ' Call Dir the first time, pointing it to all Excel files in the folder path. FileName = Dir(FolderPath & "*.xl*") ' Loop until Dir returns an empty string. Do While FileName <> "" ' Open a workbook in the folder Set WorkBk = Workbooks.Open(FolderPath & FileName) ' Set the source range to be A9 through C9. ' Modify this range for your workbooks. ' It can span multiple rows. Set SourceRange = WorkBk.Worksheets(1).Range("B2:K61") ' Set the destination range to start at column B and ' be the same size as the source range. Lc = Lastcol(DestRange) Set DestRange = SummarySheet.Range("B" & NRow) With SourceRange DestRange = DestRange.Cells(1, Lc + 1) _ .Resize(.Rows.Count, .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 ' Use Dir to get the next file name. FileName = Dir() Loop ' Call AutoFit on the destination sheet so that all ' data is readable. SummarySheet.Columns.AutoFit End Sub
Merci d'avance
Partager