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
| Function vbaDateDiff(ByVal FirstDateCell As String, ByVal SecondDateCell As String, ByVal StringCode As String) As Long
dte = FirstDateCell
dte2 = SecondDateCell
vbaDateDiff = Application.Evaluate("DATEDIF(DATEVALUE(""" & FirstDateCell & """),DATEVALUE(""" & SecondDateCell & """),""" & StringCode & """)")
End Function
Sub t()
'calcul
Dim test As Range
Dim test23 As Range
Dim diff As Integer
Dim dest As Range
Dim dte As Date
Dim jr As Integer
Dim mo As Integer
Dim an As Integer
With ActiveSheet
Set test = .Range("J2")
Set test23 = .Range("AH2")
Set dest = .Range("AQ2")
For i = 0 To .Columns(1).Find("TH*", , , , xlByColumns, xlPrevious).Row - 1
If IsDate(test.Offset(i, 0)) Then
diff = test.Offset(i, 0) - test23.Offset(i, 0)
If diff >= 0 Then
jr = vbaDateDiff(Format(test.Offset(i, 0), "mm/dd/yyyy"), Format(test23.Offset(i, 0), "mm/dd/yyyy"), "md")
mo = vbaDateDiff(Format(test.Offset(i, 0), "mm/dd/yyyy"), Format(test23.Offset(i, 0), "mm/dd/yyyy"), "ym")
an = vbaDateDiff(Format(test.Offset(i, 0), "mm/dd/yyyy"), Format(test23.Offset(i, 0), "mm/dd/yyyy"), "y")
dest.Offset(i, 0) = an
dest.Offset(i, 1) = mo
dest.Offset(i, 2) = jr
Else
jr = vbaDateDiff(Format(test23.Offset(i, 0), "mm/dd/yyyy"), Format(test.Offset(i, 0), "mm/dd/yyyy"), "md")
mo = vbaDateDiff(Format(test23.Offset(i, 0), "mm/dd/yyyy"), Format(test.Offset(i, 0), "mm/dd/yyyy"), "ym")
an = vbaDateDiff(Format(test23.Offset(i, 0), "mm/dd/yyyy"), Format(test.Offset(i, 0), "mm/dd/yyyy"), "y")
dest.Offset(i, 0) = an
dest.Offset(i, 1) = mo
dest.Offset(i, 2) = jr
End If
End If
Next i
End With
End Sub |
Partager