calculate dynamic between dates(days,months,years ) alternative datediff

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
846
Office Version
  1. 2010
hello
if is possible to calculate days, months & years between two dates actually i use function datediff but this is not dynamic if do that by macro because i have at least 1000 rows
i would the result in col c after subtract a-b
SH.xlsm
ABCDE
1dat1dat2daysmonthyears
201/01/201605/05/2017
302/01/201606/05/2017
403/02/201607/06/2017
504/02/201608/05/2017
605/02/201609/05/2017
706/02/201610/07/2017
807/02/201611/05/2017
908/02/201612/08/2017
1009/05/201613/05/2017
1110/02/201614/05/2017
1211/02/201615/09/2017
1312/04/201616/05/2017
1413/02/201617/10/2017
1514/02/201618/05/2017
1615/02/201619/05/2017
1716/02/201620/11/2017
1817/03/201621/05/2017
1918/02/201622/05/2017
2019/02/201623/12/2017
2120/02/201624/05/2017
2221/02/201625/05/2017
ورقة3
 
Last edited:

Some videos you may like

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
47,950
Office Version
  1. 365
Platform
  1. Windows
What do you mean that datedif is not dynamic?
 

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
846
Office Version
  1. 2010
hi, fluff
as far as i know it applies for specific cells not like in my case they contain many cells in col a,b so i no know if another function do that what i'm looking for
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
47,950
Office Version
  1. 365
Platform
  1. Windows
You said you wanted to calculate it between two dates which is exactly what datedif does.
What exactly are you trying to do?
 

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
846
Office Version
  1. 2010

ADVERTISEMENT

as in picture it supposes subtract between col a, b and result days in col c, months col d years in col e so far this is what i got but it doesn't success
VBA Code:
Sub ff()
Dim str, en As Date
Dim diff, diff1, dif2 As Integer
Dim lstrow As Integer
lstrow = Cells(Rows.Count, 5).End(xlUp)
str = Range("a" & lstrow).Value
en = Range("b" & lstrow).Value
diff = DateDiff("d", str, en)
diff1 = DateDiff("m", str, en)
diff2 = DateDiff("yyyy", str, en)
Range("c" & lstrow).Value = diff
Range("d" & lstrow).Value = diff1
Range("e" & lstrow).Value = diff2
End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
47,950
Office Version
  1. 365
Platform
  1. Windows
If you want to do that for the entire range, rather than just the last row, then you will have to loop through the range.
 

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
846
Office Version
  1. 2010

ADVERTISEMENT

is it important do that by loop? if is possible i prefer do that without loop to avoid slowly the code
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
47,950
Office Version
  1. 365
Platform
  1. Windows
Why not just use a formula
+Fluff v2.xlsm
ABCDE
1dat1dat2daysmonthyears
201/01/201605/05/2017490161
302/01/201606/05/2017490161
403/02/201607/06/2017490161
504/02/201608/05/2017459151
605/02/201609/05/2017459151
706/02/201610/07/2017520171
807/02/201611/05/2017459151
908/02/201612/08/2017551181
1009/05/201613/05/2017369121
1110/02/201614/05/2017459151
1211/02/201615/09/2017582191
1312/04/201616/05/2017399131
1413/02/201617/10/2017612201
1514/02/201618/05/2017459151
1615/02/201619/05/2017459151
1716/02/201620/11/2017643211
1817/03/201621/05/2017430141
1918/02/201622/05/2017459151
2019/02/201623/12/2017673221
2120/02/201624/05/2017459151
2221/02/201625/05/2017459151
Sheet3
Cell Formulas
RangeFormula
C2:C22C2=DATEDIF(A2,B2,"d")
D2:D22D2=DATEDIF(A2,B2,"m")
E2:E22E2=DATEDIF(A2,B2,"y")
 

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
846
Office Version
  1. 2010
the problem it doesn't show this function i no know if the reason is version of office
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
47,950
Office Version
  1. 365
Platform
  1. Windows
It's an old function that has been deprecated, but still works.
You can see how to use it here
 

Watch MrExcel Video

Forum statistics

Threads
1,112,865
Messages
5,542,946
Members
410,578
Latest member
schleeepy
Top