News:

MyKidsDiary.in :: Capture your kids magical moment and create your Online Private Diary for your kids

Main Menu

One Function Does It All

Started by sukishan, Sep 04, 2009, 04:41 PM

Previous topic - Next topic

sukishan

One Function Does It All
In addition to the functions described in the previous sections, VBA supplies the DatePart function. This function allows you to retrieve any portion of a date/time value and also performs some simple calculations for you. (It can retrieve the quarter of the year containing your date value, as well as all the other, simpler information.)

To call DatePart, pass to it a string indicating which information you want returned and a date value. The function returns the requested piece of information from the date value you send it. Table 2.3 lists the possible values for the DatePart function's Interval argument.

Table 2.3: Values for the Interval Argument of the DatePart Function

Setting    Description
yyyy      Year
q          Quarter
m         Month
y         Day of year
d         Day
w        Weekday
ww      Week
h         Hour
n         Minute
s         Second


For example, the following two lines of code are equivalent:

Debug.Print Day(Date)
Debug.Print DatePart("d", Date)But these two lines have no equivalent alternatives:

' Return the ordinal position of the current day within the year.
Debug.Print DatePart("y", Date)
' Return the quarter (1, 2, 3, or 4) containing today's date.

Debug.Print DatePart("q", Date)DatePart allows you to optionally specify the first day of the week (just as you can do with the WeekDay function) in its third parameter. It also allows you to optionally specify the first week of the year in its fourth parameter. (Some countries treat the week in which January 1st falls as the first week of the year, as does the United States. Other countries treat the first four-day week as the first week, and still others wait for the first full week in the year and call that the first week.)
A good beginning makes a good ending