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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| Option Explicit
Sub Concatener()
Dim Classeur_Maitre As Workbook, Classeur_Slave As Workbook
Dim oShell As Object, oFolder As Object
Dim oFolderItem As Object
Dim Tab_Files As Variant
Dim aFile As Variant
Dim ValueB7 As String
Dim Cel As Range
Application.DisplayAlerts = False
Set Classeur_Maitre = ActiveWorkbook
Set oShell = CreateObject("Shell.Application")
Set oFolder = oShell.BrowseForFolder(&H0&, "Choisir un répertoire", &H1&)
If oFolder Is Nothing Then
MsgBox "Abandon opérateur", vbCritical
Exit Sub
Else
Set oFolderItem = oFolder.Self
End If
Tab_Files = ListFilesInFolder(oFolderItem.Path, False)
For Each aFile In Tab_Files
'................................................................................................................
Set Classeur_Slave = Workbooks.Open(aFile, ReadOnly:=True)
'Ouvre classeur Slave sheet 1 et copie
Classeur_Slave.Sheets(1).Range("A2:D1000").Copy
'Copie du classeur Slave et le colle en collage spécial valeurs dans le Classeur Maitre de la sheet 1
With Classeur_Maitre.Sheets(1).Range("A65536").End(xlUp)
.Offset(1, 0).PasteSpecial Paste:=xlValues
End With
'...................................................................................................................
Classeur_Slave.Close False 'ferme le classeur Slave et boucle sur le prochain classeur Slave du répertoire
Next
Classeur_Maitre.Sheets(1).Range("A1").Activate
End Sub
Function ListFilesInFolder(strFolderName As String, Optional bIncludeSubfolders As Boolean = False, Optional strTypeFichier As String) As Variant
' necessite d'activer la reference Microsoft Scripting RunTime par VBA Outils Références cocher Microsoft Scripting RunTime
Static FSO As FileSystemObject
Static bNotFirstTime As Boolean
Static tabType As Variant, vType As Variant
Static dicoType As Object
Static strResult As String
Dim bTheFirst As Boolean
Dim oSourceFolder As Scripting.Folder
Dim oSubFolder As Scripting.Folder
Dim oFile As Scripting.File
bTheFirst = False
If Not bNotFirstTime Then
bTheFirst = True
Set FSO = CreateObject("Scripting.FileSystemObject")
Set dicoType = CreateObject("Scripting.Dictionary")
If strTypeFichier <> "" Then
tabType = Split(strTypeFichier, ";")
For Each vType In tabType
dicoType.Add vType, "Ext"
Next
End If
bNotFirstTime = True
On Error Resume Next
Set oSourceFolder = FSO.GetFolder(strFolderName)
On Error GoTo 0
If oSourceFolder Is Nothing Then
MsgBox "Le répertoir '" & strFolderName & "' n'existe pas." & vbCrLf & "L'execution va prendre fin.", vbExclamation, "Répertoir inconnu"
GoTo finApp
End If
End If
Set oSourceFolder = FSO.GetFolder(strFolderName)
For Each oFile In oSourceFolder.Files
If dicoType.Exists(ExtractFileExt(oFile.Name)) Or (strTypeFichier = "") Then
strResult = strResult & oFile.Path & ";"
End If
Next oFile
If bIncludeSubfolders Then
For Each oSubFolder In oSourceFolder.SubFolders
strResult = Join(ListFilesInFolder(oSubFolder.Path, True), ";") & ";"
Next oSubFolder
End If
If Right(strResult, 1) = ";" Then strResult = Left(strResult, Len(strResult) - 1)
ListFilesInFolder = Split(strResult, ";")
finApp:
If bTheFirst Then
Set FSO = Nothing
Set dicoType = Nothing
bNotFirstTime = False
tabType = ""
vType = ""
strResult = ""
End If
End Function
Function ExtractFileExt(strName As String) As String
If InStr(strName, ".") = 0 Then
ExtractFileExt = ""
Else
ExtractFileExt = Mid(strName, InStrRev(strName, ".") + 1)
End If
End Function |