View Single Post
  #5   Report Post  
JE McGimpsey
 
Posts: n/a
Default

While you can use worksheet functions (see my other reply), which in
general are more efficient, you could make your UDF a bit more efficient
using the DateSerial method.

This routine also corrects the result if the 1904 date system is being
used:

Public Function myEOM(BaseDate As Date, AddMonths As Long) As Date
Dim dDateCorrection As Double
With Application
If TypeOf .Caller Is Range Then _
dDateCorrection = 1462 * .Caller.Parent.Parent.Date1904
End With
myEOM = DateSerial(Year(BaseDate), Month(BaseDate) + _
AddMonths + 1, 0) + dDateCorrection
End Function


In article .com,
"Blue Hornet" wrote:

Ronni,

I had a similar problem with EOMONTH not being able to compute in the
SAME spreadsheet on the SAME computer with the SAME version of Excel.
In other words, from time to time it would just fail to compute,
leaving me with a NAME? error--and I always have the Analysis TookPak
add-in active. I didn't get it, and it was driving me crazy.

So I wrote my own function and it seems to work okay. I haven't had
occasion to check its performance on other language versions, though.
Of course, it means the sheet has to be opened with "Macros Enabled",
but all of mine require that.

*************************************

Function myEOM(Target As Date, AddMonths As Integer) As Date
' Because the EOMONTH() function in the Analysis Toolpack is causing
grief
myYear = Year(Target)
myMonth = Month(Target)
myTotalMonths = ((myYear - 1900) * 12) + myMonth + AddMonths + 1

myYear = Int(myTotalMonths / 12) + 1900
If myTotalMonths Mod 12 < 0 Then
myMonth = myTotalMonths Mod 12
Else
myMonth = 12 'Months evenly divisible by 12 means = December
myYear = Year(Target)
End If

myEOM = DateValue(myMonth & "/1/" & myYear) - 1
End Function