Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
wal wal is offline
external usenet poster
 
Posts: 19
Default Add years, months, days to a date

Excel 2003

With a worksheet function, you can add x years, y months, and z days
to a date using a formula like:

=DATE(YEAR(A1)+2, MONTH(A1)+3, DAY(A1)+4) [from Chip Pearson's site]

What is the analogous way of doing this in VBA? DATE is apparently
not a worksheet function available in VBA
(Application.WorksheetFunction). The DateAdd function in VBA lets you
add only one interval (years or months or days) at a time, and the
Help file examples don't go beyond this.

Thanks.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Add years, months, days to a date

You could look at dateserial:

dim myDate as date

with activesheet.range("A1")
mydate = dateserial(year(.value)+2, month(.value)+3,day(.value)+4)
end with

msgbox mydate

wal wrote:

Excel 2003

With a worksheet function, you can add x years, y months, and z days
to a date using a formula like:

=DATE(YEAR(A1)+2, MONTH(A1)+3, DAY(A1)+4) [from Chip Pearson's site]

What is the analogous way of doing this in VBA? DATE is apparently
not a worksheet function available in VBA
(Application.WorksheetFunction). The DateAdd function in VBA lets you
add only one interval (years or months or days) at a time, and the
Help file examples don't go beyond this.

Thanks.


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,365
Default Add years, months, days to a date

I like Dave Peterson's solution - it's the one I'd go with (actually pretty
much what I use these days), but I'll give you an old function I wrote long
ago that comes up with the same results and gives you some insight into what
all has to go on internally both in the worksheet function from Chip's site
and in the dateserial() function that Dave put up.

This function emulates the formula you got at cpearson.com and should give
the same results given the same inputs. I've added some explanation of the
"why" of some things, such as dealing with the added months.

Function AddToDate(startDate As Date, addYears As Integer, _
addMonths As Integer, addDays As Integer) As Date

Dim newYear As Integer
Dim tempMonth As Integer
Dim newMonth As Integer
Dim newDay As Integer

'break startDate into component parts
'adding years to add as part of 1st step
newYear = Year(startDate) + addYears
newMonth = Month(startDate)
newDay = Day(startDate)
'month is difficult, may cause a
'rollover to another year
tempMonth = newMonth + addMonths
'increment newYear by years worth of
'months added
newYear = newYear + Int(tempMonth / 12)
'use MOD math to determine what month
'the added months creates
newMonth = tempMonth Mod 12
'12 Mod 12 = 0, so if result was
'0 the month is December
If newMonth = 0 Then
newMonth = 12
End If
'put it all back together as new date
AddToDate = DateSerial(newYear, newMonth, newDay + addDays)

End Function


"wal" wrote:

Excel 2003

With a worksheet function, you can add x years, y months, and z days
to a date using a formula like:

=DATE(YEAR(A1)+2, MONTH(A1)+3, DAY(A1)+4) [from Chip Pearson's site]

What is the analogous way of doing this in VBA? DATE is apparently
not a worksheet function available in VBA
(Application.WorksheetFunction). The DateAdd function in VBA lets you
add only one interval (years or months or days) at a time, and the
Help file examples don't go beyond this.

Thanks.

  #4   Report Post  
Posted to microsoft.public.excel.programming
wal wal is offline
external usenet poster
 
Posts: 19
Default Add years, months, days to a date

On Aug 31, 6:13*pm, Dave Peterson wrote:
You could look at dateserial:

dim myDate as date

with activesheet.range("A1")
* *mydate = dateserial(year(.value)+2, month(.value)+3,day(.value)+4)
end with

msgbox mydate





wal wrote:

Excel 2003


With a worksheet function, you can add x years, y months, and z days
to a date using a formula like:


=DATE(YEAR(A1)+2, MONTH(A1)+3, DAY(A1)+4) [from Chip Pearson's site]


What is the analogous way of doing this in VBA? *DATE is apparently
not a worksheet function available in VBA
(Application.WorksheetFunction). *The DateAdd function in VBA lets you
add only one interval (years or months or days) at a time, and the
Help file examples don't go beyond this.


Thanks.


--

Dave Peterson- Hide quoted text -

- Show quoted text -


Thanks! I guess dateserial wasn't mnemonic enough for me to pick it
up when I did a Help file search.
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Add years, months, days to a date

And just so you are aware of an additional option, if you are doing your
addition one date part at a time (in other words, just days or just months
etc.), there is a DateAdd function available where you can specify which
date part and how many of them you are adding (use a negative quantity for
subtraction). What is nice about the DateAdd function is that you are not
limited to only years, months and days... you can add quarters and weeks as
well. In addition, instead of having to rely on the TimeSerial function to
perform the same functionality with time values as DateSerial does with date
values, there are options available for using DateAdd with time values too
(although one time part at a time). Look DateAdd up in the help files for
complete information.

--
Rick (MVP - Excel)


"wal" wrote in message
...
Excel 2003

With a worksheet function, you can add x years, y months, and z days
to a date using a formula like:

=DATE(YEAR(A1)+2, MONTH(A1)+3, DAY(A1)+4) [from Chip Pearson's site]

What is the analogous way of doing this in VBA? DATE is apparently
not a worksheet function available in VBA
(Application.WorksheetFunction). The DateAdd function in VBA lets you
add only one interval (years or months or days) at a time, and the
Help file examples don't go beyond this.

Thanks.




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Add years, months, days to a date

This works perfectly.

Is there a similar way to handle negative dates, omitting weekends and
holidays?

"JLatham" wrote:

I like Dave Peterson's solution - it's the one I'd go with (actually pretty
much what I use these days), but I'll give you an old function I wrote long
ago that comes up with the same results and gives you some insight into what
all has to go on internally both in the worksheet function from Chip's site
and in the dateserial() function that Dave put up.

This function emulates the formula you got at cpearson.com and should give
the same results given the same inputs. I've added some explanation of the
"why" of some things, such as dealing with the added months.

Function AddToDate(startDate As Date, addYears As Integer, _
addMonths As Integer, addDays As Integer) As Date

Dim newYear As Integer
Dim tempMonth As Integer
Dim newMonth As Integer
Dim newDay As Integer

'break startDate into component parts
'adding years to add as part of 1st step
newYear = Year(startDate) + addYears
newMonth = Month(startDate)
newDay = Day(startDate)
'month is difficult, may cause a
'rollover to another year
tempMonth = newMonth + addMonths
'increment newYear by years worth of
'months added
newYear = newYear + Int(tempMonth / 12)
'use MOD math to determine what month
'the added months creates
newMonth = tempMonth Mod 12
'12 Mod 12 = 0, so if result was
'0 the month is December
If newMonth = 0 Then
newMonth = 12
End If
'put it all back together as new date
AddToDate = DateSerial(newYear, newMonth, newDay + addDays)

End Function


"wal" wrote:

Excel 2003

With a worksheet function, you can add x years, y months, and z days
to a date using a formula like:

=DATE(YEAR(A1)+2, MONTH(A1)+3, DAY(A1)+4) [from Chip Pearson's site]

What is the analogous way of doing this in VBA? DATE is apparently
not a worksheet function available in VBA
(Application.WorksheetFunction). The DateAdd function in VBA lets you
add only one interval (years or months or days) at a time, and the
Help file examples don't go beyond this.

Thanks.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting days to years & months & days oldLearner57 Excel Discussion (Misc queries) 4 October 2nd 09 01:57 PM
how do i convert a number of days to years, months & days? SafetyLen Excel Discussion (Misc queries) 1 August 23rd 07 01:34 AM
HOW DO I CALCULATE THE YEARS MONTHS AND DAYS BETWEEN SEVERAL DATE Susan Excel Discussion (Misc queries) 1 June 29th 06 02:27 PM
FORMULA FOR CALCULATING YEARS, DAYS, & MONTHS BETWEEN SEVERAL DATE Sam Excel Discussion (Misc queries) 3 June 28th 06 10:34 PM
Start Date minus (Years + Months + Days) = ? quartz[_2_] Excel Programming 3 May 11th 06 06:05 PM


All times are GMT +1. The time now is 01:51 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"