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
| Private Sub Worksheet_Change(ByVal Target As Range)
Dim Dte As Date
Dim Plage As Range, c As Range
Dim i As Integer, n As Integer
Dim Ligne As Long
If Target.Address = "$C$4" Then
If Target.Value <> "" And IsDate(Target.Value) Then
Dte = Target.Value
Set Plage = Range("A19", Cells(Rows.Count, "A").End(xlUp))
Dte = IIf(DateDiff("d", Dte, Application.Min(Plage)) > 0, Application.Min(Plage), Dte)
Dte = IIf(DateDiff("d", Dte, Application.Max(Plage)) < 0, Application.Max(Plage), Dte)
Do
Ligne = RechercheDate(Plage, DateAdd("d", n, Dte))
If Ligne > 0 Then Exit Do
Ligne = RechercheDate(Plage, DateAdd("d", -n, Dte))
If Ligne > 0 Then Exit Do
n = n + 1
Loop
Set Plage = Nothing
End If
End If
Application.EnableEvents = False
Range("C5").Value = IIf(Ligne = 0, "", Ligne)
Application.EnableEvents = True
End Sub
Function RechercheDate(LaPlage As Range, LaDate As Date) As Long
Dim c As Range
Set c = LaPlage.Find(LaDate)
If Not c Is Nothing Then
RechercheDate = c.Row
Set c = Nothing
End If
End Function |
Partager