Bonjour David et bonjour au Forum,
je te propose le code suivant (certains vont exploser de rire en le voyant, car je bidouille plutot que développe)
Mais, bon, chez moi ça marche
ce code à très largement été inspiré par ce fil:
http://www.excel-downloads.com/html/French/forum/messages/1_25473_25473.htm
Bonne journée
--------------------------------
Option Explicit
Sub Lire_repertoire()
Dim i As Integer, fichier As String, nom As String
Chemin = ThisWorkbook.Path
TailleChemin = Len(Chemin)
With Application.FileSearch
.NewSearch:
.LookIn = Chemin
.SearchSubFolders = True:
.MatchTextExactly = False:
.FileType = msoFileTypeAllFiles
'petit menage avant de commencer
Range("A1:B9").Select
Selection.ClearContents
Pommes = 0
Poires = 0
Pruneaux = 0
If .Execute(msoSortOrderDescending) > 0 Then
For i = 1 To .FoundFiles.Count
tailleCheminFichier = Len(.FoundFiles(i))
fichier = Mid(.FoundFiles(i), (TailleChemin + 2), tailleCheminFichier)
nom = Left(fichier, (Len(fichier) - 4))
'test si fichier excel
If Right(fichier, 3) = "xls" Then
'test si différent du fichier Stat
If nom <> "Stat" Then
'fonctionne si le classeur lu n'a qu'une feuille
'mais ne fonctionne pas si le classeur lu a plusieurs onglets
Cells(2, 1).FormulaR1C1 = _
("='" & Chemin & "\" & fichier & " '!RC[1]")
Pommes = Pommes + Cells(2, 1)
Cells(3, 1).FormulaR1C1 = _
("='" & Chemin & "\" & fichier & " '!RC[1]")
Poires = Poires + Cells(3, 1)
'fonctionne si le classeur lu à plusieurs onglets
'il faut alors remplacer "Feuil1" par le nom de l'onglet
Cells(4, 1).FormulaR1C1 = _
("='" & Chemin & "\[" & fichier & "]" & "Feuil1'!RC[1]")
Pruneaux = Pruneaux + Cells(4, 1)
End If
End If
Next i
Cells(2, 1) = "Pommes"
Cells(2, 2) = Pommes
Cells(3, 1) = "Poires"
Cells(3, 2) = Poires
Cells(4, 1) = "Pruneaux"
Cells(4, 2) = Pruneaux
Else
MsgBox "y'a pas de fichiers"
End If
End With
End Sub