Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Adding months to date at end of month | Excel Discussion (Misc queries) | |||
Adding .45 months to a date | Excel Worksheet Functions | |||
Adding months to a date | Excel Worksheet Functions | |||
Adding 6 Months to a Date | Excel Worksheet Functions | |||
Adding 6 months to any given date | New Users to Excel |