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
| Option Explicit
Sub test()
Dim myPath As String, myFile As String, myPassword As String
Dim maFeuil As Worksheet
Dim oDate As Date
Dim oCount As Long
'Défini le chemin
myPath = "C:\Users\...\Desktop\test"
myFile = Dir(myPath & "\*.*")
With Worksheets("Feuil1")
Do While myFile <> ""
oDate = DateSerial(Left(myFile, 4), Mid(myFile, 5, 2), Mid(myFile, 7, 2))
If oDate >= .Range("A2") And oDate <= .Range("B2") Then
Call ClasseurOuvert(myPath & "\" & myFile)
If Right(myFile, 4) Like ".xls*" Then
With Workbooks(myFile)
If FeuilleExiste("Sheet") Then
Set maFeuil = .Worksheets("Sheet")
oCount = ma_fonction(maFeuil)
Else
MsgBox "Le classeur " & myFile & " ne présente pas d'onglets ""Sheet""."
End If
.Close True
End With
Else
MsgBox "Le fichier " & myFile & " n'est pas une fichier Excel."
End If
.Cells(Rows.Count, 4).End(xlUp).Offset(1, 0) = oDate
.Cells(Rows.Count, 5).End(xlUp).Offset(1, 0) = oCount
End If
myFile = Dir()
Loop
End With
End Sub
Function ClasseurOuvert(NomFich)
On Error Resume Next
Workbooks(NomFich).Activate
If Err <> 0 Then Workbooks.Open Filename:=NomFich
On Error GoTo 0
End Function
Function FeuilleExiste(NomFeuille) As Boolean
Dim f As Object
On Error Resume Next
Set f = Sheets(NomFeuille)
If Err = 0 Then FeuilleExiste = True
Set f = Nothing
End Function
Function ma_fonction(oWksh As Worksheet)
Dim oVal As Long
Dim oRng As Range
Dim i As Integer
oVal = 0
With oWksh
Set oRng = .Range("S1")
For i = 1 To .Cells(Rows.Count, oRng.Column).End(xlUp).Row - 1
If oRng.Offset(i, 8) = "O" Then
oVal = oVal + oRng.Offset(i, 0)
End If
Next i
End With
ma_fonction = oVal
End Function |
Partager