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

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,507
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
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:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
What do you mean that datedif is not dynamic?
 
Upvote 0
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
 
Upvote 0
You said you wanted to calculate it between two dates which is exactly what datedif does.
What exactly are you trying to do?
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0
is it important do that by loop? if is possible i prefer do that without loop to avoid slowly the code
 
Upvote 0
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")
 
Upvote 0
the problem it doesn't show this function i no know if the reason is version of office
 
Upvote 0
It's an old function that has been deprecated, but still works.
You can see how to use it here
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top