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
| Private Sub CommandButton1_Click()
Dim LastLig As Long, i As Long, k As Long
Dim DD As Date, DF As Date, DPm As Date, FPm As Date, DPs As Date, FPs As Date
Dim Tb, Res()
With Sheets("données") 'A adapter au nom de ta feuille
'DD = CDate(.Range("E1").Value) 'Date de début
'DF = CDate(.Range("G1").Value) 'Date fin
DPm = .Range("e2").Value 'Heure Début Pointe Matin
FPm = .Range("e3").Value 'Heure Fin Point Matin
DPs = .Range("e4").Value 'Heure Début Point Soir
FPs = .Range("e5").Value 'Heure Fin Point Soir
LastLig = .Cells(.Rows.Count, "A").End(xlUp).Row
Tb = .Range("A2:B" & LastLig).Value 'A adapter aux colonnes de données, ici A:B
ReDim Res(1 To 2, 1 To 1)
'On remplit la ligne des titres
Res(1, 1) = .Cells(1, 1): Res(2, 1) = .Cells(1, 2): k = 1
For i = 1 To UBound(Tb)
'Si Tb(i,1) est une date
If IsDate(Tb(i, 1)) Then
'Si la date Tb(i,1) est comprise en Date début et Date Fin
'If DateDiff("d", DD, Tb(i, 1)) >= 0 And DateDiff("d", DF, Tb(i, 1)) <= 0 Then
'Si la date Tb(i,1) est janvier ou février ou décembre
If Month(Tb(i, 1) = 1) Or Month(Tb(i, 1) = 2) Or Month(Tb(i, 1) = 12) Then
'Si la date Tb(i,1) n'est pas un dimanche
If Weekday(Tb(i, 1), vbMonday) < 7 Then
'Si lheure est comprise dans les intervales de pointes
If Interv(Tb(i, 1), DPm, FPm) Or Interv(Tb(i, 1), DPs, FPs) Then
'On remplit le tableau Res
k = k + 1
ReDim Preserve Res(1 To UBound(Tb, 2), 1 To k)
Res(1, k) = CDbl(Tb(i, 1))
Res(2, k) = Tb(i, 2)
End If
End If
End If
End If
Next i
End With
With Sheets("pointes")
.UsedRange.Clear
With .Range("A1").Resize(UBound(Res, 2), 2)
.Value = Application.Transpose(Res)
.NumberFormat = "dd/mm/yyyy hh:mm"
End With
Sheets("pointes").Select
Range("b2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.NumberFormat = Number
End With
End Sub
Private Function Interv(ByVal H As Date, Hd As Date, Hf As Date) As Boolean
Dim Hr As Date
Hr = TimeSerial(Hour(H), Minute(H), 0)
Interv = DateDiff("n", Hd, Hr) >= 0 And DateDiff("n", Hf, Hr) <= 0
End Function |