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
| Function SearchFile(strDir As String, Optional strExt = "*.*") As String
'Retourne le nom des fichiers presents dans un dossier passe en parametre(strDir),
'en fonction de l'extension recherchée (strExt)
'La recherche ne se fait pas dans les sous répertoires
'Ex : filesToSearch = SearchFile("C:\","*.xls") -> on recherche toous les fichiers excel presents dans c:
'retourne un resultat de la forme fichier1;fichier2;....
On Error GoTo ErrSearchFile
Dim i As Integer
Dim file As String
With Application.FileSearch
.NewSearch
.LookIn = strDir
.FileName = strExt
If .Execute(SortBy:=msoSortByFileName, SortOrder:=msoSortOrderAscending) > 0 Then
For i = 1 To .FoundFiles.Count
file = FileName(.FoundFiles(i))
SearchFile = SearchFile & file & ";"
Next i
SearchFile = Left(SearchFile, Len(SearchFile) - 1)
End If
End With
Exit Function
ErrSearchFile:
SearchFile = ""
MsgBox Err.Number & vbCrLf & vbCrLf & Err.Description, vbCritical
End Function
Function FileName(strFullPath As String) As String
'retourne un nom de fichier en fonction du chemin complet
Dim result As Variant
Dim i As Integer
result = Split(strFullPath, "\")
FileName = result(UBound(result))
End Function
Sub CopyDatas(strCurWorkBookName As String, strWorkBookPath As String)
'copie les données d'un classeur vers le classeur courant
Dim rRangeToCopy As Range
Dim iNumColonne As Integer
'copie des données
Workbooks.Open (strWorkBookPath)
rRangeToCopy = Range("B1:B6")
Workbooks.Close
Windows(strCurWorkBookName).Activate
iNumColonne = ActiveWorkbook.Sheets(1).Cells(1, 255).End(xlToRight).Column
ActiveWorkbook.Sheets(1).Cells(1, iNumColonne) = rRangeToCopy
End Sub
Sub main()
Dim strCurWorkBookName As String
Dim vAllFiles As Variant
Dim strDir As String
Dim i As Integer
'recupere le nom du workbook courant
strCurWorkBookName = ActiveWorkbook.Name
strDir = "Ton repertoire ou se situent les fichiers"
'recupere l'ensemble des fichiers
vAllFiles = SearchFile(strDir, "*.xls")
For i = LBound(vAllFiles) To UBound(vAllFiles)
Call CopyDatas(strCurWorkBookName, vAllFiles(i))
Next i
End Sub |
Partager