Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
kdw kdw is offline
external usenet poster
 
Posts: 13
Default Adding months to a Date in VBA

I need to add months to a date. For example, 4/2/04 + 6 months to give
10/2/04.

I have seen examples of how to add months such as:

=MIN(DATE(YEAR(A1),MONTH(A1)+48,DAY(A1));DATE(YEAR (A1),MONTH(A1)+1+48,0))

But Date() means something different in VBA than in Excel. Is there another
solution?

Thanks.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,092
Default Adding months to a Date in VBA

Take a look at Chip Pearson's site about dates
http://www.cpearson.com/excel/datearith.htm

Mike F
"kdw" wrote in message
...
I need to add months to a date. For example, 4/2/04 + 6 months to give
10/2/04.

I have seen examples of how to add months such as:

=MIN(DATE(YEAR(A1),MONTH(A1)+48,DAY(A1));DATE(YEAR (A1),MONTH(A1)+1+48,0))

But Date() means something different in VBA than in Excel. Is there

another
solution?

Thanks.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Adding months to a Date in VBA

Here's one way

Function DateTest(NumMonths)
Dim Date1, Date2, Date3

Date1 = Range("A1").Value
Date2 = DateSerial(Year(Date1), Month(Date1) + NumMonths, Day(Date1))
Date3 = DateSerial(Year(Date1), Month(Date1) + NumMonths + 1, 0)


'=MIN(DATE(YEAR(A1),MONTH(A1)+48,DAY(A1)),DATE(YEA R(A1),MONTH(A1)+1+48,0))
If Date2 < Date3 Then
DateTest = Date2
Else
DateTest = Date3
End If
End Function


--

HTH

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


"kdw" wrote in message
...
I need to add months to a date. For example, 4/2/04 + 6 months to give
10/2/04.

I have seen examples of how to add months such as:

=MIN(DATE(YEAR(A1),MONTH(A1)+48,DAY(A1));DATE(YEAR (A1),MONTH(A1)+1+48,0))

But Date() means something different in VBA than in Excel. Is there

another
solution?

Thanks.



  #4   Report Post  
Posted to microsoft.public.excel.programming
kdw kdw is offline
external usenet poster
 
Posts: 13
Default Adding months to a Date in VBA

The DateSerial function is exactly what I needed. Thanks.

"Bob Phillips" wrote:

Here's one way

Function DateTest(NumMonths)
Dim Date1, Date2, Date3

Date1 = Range("A1").Value
Date2 = DateSerial(Year(Date1), Month(Date1) + NumMonths, Day(Date1))
Date3 = DateSerial(Year(Date1), Month(Date1) + NumMonths + 1, 0)


'=MIN(DATE(YEAR(A1),MONTH(A1)+48,DAY(A1)),DATE(YEA R(A1),MONTH(A1)+1+48,0))
If Date2 < Date3 Then
DateTest = Date2
Else
DateTest = Date3
End If
End Function


--

HTH

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


"kdw" wrote in message
...
I need to add months to a date. For example, 4/2/04 + 6 months to give
10/2/04.

I have seen examples of how to add months such as:

=MIN(DATE(YEAR(A1),MONTH(A1)+48,DAY(A1));DATE(YEAR (A1),MONTH(A1)+1+48,0))

But Date() means something different in VBA than in Excel. Is there

another
solution?

Thanks.




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 863
Default Adding months to a Date in VBA

The corresponding VBA function is DateSerial. But there are also specialized
functions like DateAdd, DatePart, etc. Check VBA help for date functions.

On Wed, 17 Nov 2004 15:29:06 -0800, kdw wrote:

I need to add months to a date. For example, 4/2/04 + 6 months to give
10/2/04.

I have seen examples of how to add months such as:

=MIN(DATE(YEAR(A1),MONTH(A1)+48,DAY(A1));DATE(YEA R(A1),MONTH(A1)+1+48,0))

But Date() means something different in VBA than in Excel. Is there another
solution?

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
Adding months to date at end of month Shaggyjh Excel Discussion (Misc queries) 4 November 28th 07 12:30 PM
Adding .45 months to a date Trev[_2_] Excel Worksheet Functions 4 April 25th 07 05:31 PM
Adding months to a date miss misty Excel Worksheet Functions 3 April 13th 07 01:59 AM
Adding 6 Months to a Date Excel User Excel Worksheet Functions 8 January 25th 07 12:16 AM
Adding 6 months to any given date hoyt New Users to Excel 7 July 9th 06 11:14 AM


All times are GMT +1. The time now is 11:14 AM.

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

About Us

"It's about Microsoft Excel"