Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5
Default FORMULA NEEDED FOR MONTHS

Hi,

There's no formula that I know of that will cater for what you are looking
for. What you can use is a VBA function to calculate the month difference.
Try something like the below function (paste the below into a module)

'To use on an Excel worksheet type =Months(A1,D1) where A1 represents the
first date
'and D1 represents the second date

Public Function Months(Date1 As Date, Date2 As Date) As Long

Dim iMonthCount As Long, sYear As Long, eYear As Long, sMonth As Long,
eMonth As Long

sMonth = Month(Date1)
eMonth = Month(Date2)
sYear = Year(Date1)
eYear = Year(Date2)

If sMonth = eMonth Then
iMonthCount = Abs(sMonth - eMonth)
If sYear eYear Then
iMonthCount = ((sYear - eYear) * 12) + iMonthCount
ElseIf sYear < eYear Then
iMonthCount = ((Abs(sYear - eYear)) * 12) - iMonthCount
End If
ElseIf eMonth sMonth Then
iMonthCount = Abs(eMonth - sMonth)
If sYear eYear Then
iMonthCount = ((sYear - eYear) * 12) - iMonthCount
ElseIf sYear < eYear Then
iMonthCount = ((Abs(sYear - eYear)) * 12) + iMonthCount
End If
End If

Months = iMonthCount

End Function

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...excel/200906/1

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 7,247
Default FORMULA NEEDED FOR MONTHS

No need for the VBA code. There is an undocumented function in Excel
called DATEDIF that will calculate the interval between two dates in a
variety of intervals. E.g.,

=DATEDIF(StartDate,EndDate,"m")

This is not to be confused with the VBA DateDiff function. For more
info about DATEDIF, see www.cpearson.com/Excel/DateDif.aspx

For reasons known only to Microsoft, they don't document this rather
useful function.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


On Sat, 20 Jun 2009 13:28:48 GMT, "Alan McQ via OfficeKB.com"
<u22393@uwe wrote:

Hi,

There's no formula that I know of that will cater for what you are looking
for. What you can use is a VBA function to calculate the month difference.
Try something like the below function (paste the below into a module)

'To use on an Excel worksheet type =Months(A1,D1) where A1 represents the
first date
'and D1 represents the second date

Public Function Months(Date1 As Date, Date2 As Date) As Long

Dim iMonthCount As Long, sYear As Long, eYear As Long, sMonth As Long,
eMonth As Long

sMonth = Month(Date1)
eMonth = Month(Date2)
sYear = Year(Date1)
eYear = Year(Date2)

If sMonth = eMonth Then
iMonthCount = Abs(sMonth - eMonth)
If sYear eYear Then
iMonthCount = ((sYear - eYear) * 12) + iMonthCount
ElseIf sYear < eYear Then
iMonthCount = ((Abs(sYear - eYear)) * 12) - iMonthCount
End If
ElseIf eMonth sMonth Then
iMonthCount = Abs(eMonth - sMonth)
If sYear eYear Then
iMonthCount = ((sYear - eYear) * 12) - iMonthCount
ElseIf sYear < eYear Then
iMonthCount = ((Abs(sYear - eYear)) * 12) + iMonthCount
End If
End If

Months = iMonthCount

End Function

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
3 months prior and 3 months post a date renee Excel Worksheet Functions 2 May 2nd 08 05:46 PM
Formula to ave. last 3 months [email protected] Excel Worksheet Functions 2 March 24th 07 04:19 AM
How to use months in formula. Dave Dobson Excel Worksheet Functions 15 October 12th 06 05:22 PM
Adding months to dates should account for 28-30-31 day months Graham Excel Worksheet Functions 3 February 1st 06 12:06 PM
need if formula for 12 months blopreste3180 Excel Worksheet Functions 3 January 19th 06 05:40 PM


All times are GMT +1. The time now is 12:53 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"