ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Subtracting Dates VBA (https://www.excelbanter.com/excel-programming/332318-subtracting-dates-vba.html)

Jean-Jérôme Doucet via OfficeKB.com

Subtracting Dates VBA
 
Hi,

I want to subtract two dates 'YYYYY-MM-DD' but by doing in VBA and pointing
to their respective cells and dump the result in another cell.

For example, I am working on this experiment :

Quote:

Sub datescalcul()
Dim Baux As Double
Baux = Application.DATEDIF(Worksheets("Formulaire").Range ("I16").Value,
Worksheets("Formulaire").Range("J16").Value, "m") / 12
sheets("Formulaire").Range("K16").Value = Baux
End Sub
But it does not work. Maye it is that dateDif Excel function cannot be
converted in VBA function?
Thx!

JJD

--
Message posted via http://www.officekb.com

Niek Otten

Subtracting Dates VBA
 
"does not work" does not give much information.
But in VBA, it is DATEDIFF (2 F's)

Niek Otten
"Jean-Jerome Doucet via OfficeKB.com" wrote in
message ...
Hi,

I want to subtract two dates 'YYYYY-MM-DD' but by doing in VBA and
pointing
to their respective cells and dump the result in another cell.

For example, I am working on this experiment :

Quote:

Sub datescalcul()
Dim Baux As Double
Baux = Application.DATEDIF(Worksheets("Formulaire").Range ("I16").Value,
Worksheets("Formulaire").Range("J16").Value, "m") / 12
sheets("Formulaire").Range("K16").Value = Baux
End Sub


But it does not work. Maye it is that dateDif Excel function cannot be
converted in VBA function?
Thx!

JJD

--
Message posted via http://www.officekb.com




Niek Otten

Subtracting Dates VBA
 
So that is without "Application."

"Niek Otten" wrote in message
...
"does not work" does not give much information.
But in VBA, it is DATEDIFF (2 F's)

Niek Otten
"Jean-Jerome Doucet via OfficeKB.com" wrote in
message ...
Hi,

I want to subtract two dates 'YYYYY-MM-DD' but by doing in VBA and
pointing
to their respective cells and dump the result in another cell.

For example, I am working on this experiment :

Quote:

Sub datescalcul()
Dim Baux As Double
Baux = Application.DATEDIF(Worksheets("Formulaire").Range ("I16").Value,
Worksheets("Formulaire").Range("J16").Value, "m") / 12
sheets("Formulaire").Range("K16").Value = Baux
End Sub


But it does not work. Maye it is that dateDif Excel function cannot be
converted in VBA function?
Thx!

JJD

--
Message posted via http://www.officekb.com






Jean-Jerome Doucet via OfficeKB.com

Subtracting Dates VBA
 
You are right, my apologies for the lack of details.

I forgot to mention that the error is on my subtraction. It says property or
method not managed by this object. And I want to return a numer of years and
have decimal or where we are in the last years (e.g. 3 and a half year 3,50).

Here's the way it is supposed to work :
Oh and it is J16 - I16 (upper date - lower date).

Werner

--
Message posted via http://www.officekb.com

Jean-Jerome Doucet via OfficeKB.com

Subtracting Dates VBA
 
Ops wrong name (I use Werner as a nickname on other forums but I use my real
name on this one.)

Jean-Jérôme Doucet

--
Message posted via http://www.officekb.com

Bob Phillips[_6_]

Subtracting Dates VBA
 
It is also a different order

With Worksheets("Formulaire")
Baux = DateDiff("m", .Range("I16").Value, .Range("J16").Value) / 12
End With

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Jean-Jerome Doucet via OfficeKB.com" wrote in
message ...
Hi,

I want to subtract two dates 'YYYYY-MM-DD' but by doing in VBA and

pointing
to their respective cells and dump the result in another cell.

For example, I am working on this experiment :

Quote:

Sub datescalcul()
Dim Baux As Double
Baux = Application.DATEDIF(Worksheets("Formulaire").Range ("I16").Value,
Worksheets("Formulaire").Range("J16").Value, "m") / 12
sheets("Formulaire").Range("K16").Value = Baux
End Sub


But it does not work. Maye it is that dateDif Excel function cannot be
converted in VBA function?
Thx!

JJD

--
Message posted via http://www.officekb.com




Jean-Jerome Doucet via OfficeKB.com

Subtracting Dates VBA
 
Thank you a lot Bob!!

I adjusted a bit the code for my needs and i works perfectly!

Have a good day!

Werner

Quote:

Sub datescalcul()

Dim Baux As Double
Dim I As Long

Let lstRw = Sheets("Formulaire").Range("a65536").End(xlUp).Row

For I = 16 To lstRw
With Worksheets("Formulaire")
Baux = DateDiff("m", Sheets("Formulaire").Range("I" & I).Value, Sheets
("Formulaire").Range("J" & I).Value) / 12
Sheets("Formulaire").Range("K" & I).Value = Baux
End With

Next I


End Sub
--
Message posted via http://www.officekb.com


All times are GMT +1. The time now is 02:10 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com