"Lyle" wrote in message
...
Hello. I am fairly familiar with Excel and have been racking my brain
trying
to figure a formula out. Here is the scenario. The military uses dates
in
this order yyyymmdd ( i.e. 20050401 is April 1, 2005). I have two colums
of
data in this fomat and I want to subtract one from the other to give me
atleast a total number of months and years. For example 19971015 minus
19600212. The long hand answer is 37y08m03d. I have 3190 of these I must
compute. Any help out there?
--
Lyle
I have created a simple UDF which works for the example you gave. Start the
Visual Basic Editor, insert a module and paste this
Public Function MilDateDiff(ByVal date1 As String, date2 As String) As
String
Dim d1 As Date
Dim d2 As Date
Dim sDiff As String
d1 = DateSerial(Left(date1, 4), Mid(date1, 5, 2), Right(date1, 2))
d2 = DateSerial(Left(date2, 4), Mid(date2, 5, 2), Right(date2, 2))
sDiff = CStr(Year(d2) - Year(d1))
sDiff = sDiff + "y"
sDiff = sDiff + CStr(Month(d2) - Month(d1))
sDiff = sDiff + "m"
sDiff = sDiff + CStr(Day(d2) - Day(d1))
sDiff = sDiff + "d"
MilDateDiff = sDiff
End Function
If you enter =MilDateDiff(19600212, 19971015) this function returns 37y8m3d.
This is slightly different from 37y08m03d
/Fredrik
|