You are on page 1of 2

Function XDATE(y, m, d, Optional fmt As String) As String

If IsMissing(fmt) Then fmt = "Short Date"


XDATE = Format(DateSerial(y, m, d), fmt)
End Function
Function XDATEADD(xdate1, days, Optional fmt As String) As String
Dim TempDate As Date
If IsMissing(fmt) Then fmt = "Short Date"
xdate1 = RemoveDay(xdate1)
TempDate = DateValue(xdate1)
XDATEADD = Format(TempDate + days, fmt)
End Function
Function XDATEDIF(xdate1, xdate2) As Long
xdate1 = RemoveDay(xdate1)
xdate2 = RemoveDay(xdate2)
XDATEDIF = DateValue(xdate1) - DateValue(xdate2)
End Function
Function XDATEYEARDIF(xdate1, xdate2) As Long
Dim YearDiff As Long
xdate1 = RemoveDay(xdate1)
xdate2 = RemoveDay(xdate2)
YearDiff = Year(xdate2) - Year(xdate1)
If DateSerial(Year(xdate1), Month(xdate2), Day(xdate2)) < CDate(xdate1) Then
YearDiff = YearDiff - 1
XDATEYEARDIF = YearDiff
End Function
Function XDATEYEAR(xdate1)
xdate1 = RemoveDay(xdate1)
XDATEYEAR = Year(DateValue(xdate1))
End Function
Function XDATEMONTH(xdate1)
xdate1 = RemoveDay(xdate1)
XDATEMONTH = Month(DateValue(xdate1))
End Function
Function XDATEDAY(xdate1)
xdate1 = RemoveDay(xdate1)
XDATEDAY = Day(DateValue(xdate1))
End Function
Function XDATEDOW(xdate1)
xdate1 = RemoveDay(xdate1)
XDATEDOW = Weekday(xdate1)
End Function
Private Function RemoveDay(xdate1)

' Remove day of week from string


Dim i As Integer
Dim Temp As String
Temp = xdate1
For i = 0 To 6 'Unabbreviated day names
Temp = Application.Substitute(Temp, Format(DateSerial(1900, 1, 0), "dddd"), "")
Next i
For i = 0 To 6 'Abbreviated day names
Temp = Application.Substitute(Temp, Format(DateSerial(1900, 1, 0), "ddd"), "")
Next i
RemoveDay = Temp
End Function
Sub SetMacroOptions()
' Add descriptions, and put in the Date & Time function category
Dim HelpFile As String
HelpFile = ThisWorkbook.Path & "\xdate.hlp"
' On Error Resume Next
' With Application
'
.MacroOptions macro:="XDATE", Description:="(ADD-IN FUNCTION) Returns
a date for any year between 0100 and 9999. fmt is an optional date formatting string.",
Category:=2, HelpContextID:=200, HelpFile:=HelpFile
'
.MacroOptions macro:="XDATEADD", Description:="(ADD-IN FUNCTION)
Returns a date, incremented by a specified number of days. fmt is an optional date
formatting string.", Category:=2, HelpContextID:=300, HelpFile:=HelpFile
'
.MacroOptions macro:="XDATEDIF", Description:="(ADD-IN FUNCTION)
Returns the number of days between date1 and date2 (date1-date2).", Category:=2,
HelpContextID:=400, HelpFile:=HelpFile
'
.MacroOptions macro:="XDATEYEARDIF", Description:="(ADD-IN
FUNCTION) Returns the number of full years between date1 and date2 (date1-date2).
Useful for calculating ages.", Category:=2, HelpContextID:=500, HelpFile:=HelpFile
'
.MacroOptions macro:="XDATEYEAR", Description:="(ADD-IN FUNCTION)
Returns the year for a date.", Category:=2, HelpContextID:=600, HelpFile:=HelpFile
'
.MacroOptions macro:="XDATEMONTH", Description:="(ADD-IN
FUNCTION) Returns the month for a date.", Category:=2, HelpContextID:=700,
HelpFile:=HelpFile
'
.MacroOptions macro:="XDATEDAY", Description:="(ADD-IN FUNCTION)
Returns the day for a date.", Category:=2, HelpContextID:=800, HelpFile:=HelpFile
'
.MacroOptions macro:="XDATEDOW", Description:="(ADD-IN FUNCTION)
Returns an integer corresponding to the weekday for a date (1=Sunday).", Category:=2,
HelpContextID:=900, HelpFile:=HelpFile
' End With
'If GetSetting(APPNAME:="JWalk", section:="ExtendedDateFunctions",
Key:="FunctionMsg", Default:=1) = 1 Then UserForm1.Show
End Sub
Y ADEMAS EN THIS WORKBOOK
Private Sub Workbook_Open()
SetMacroOptions
End Sub

You might also like