
- VBA Tutorial
- VBA - Home
- VBA - Overview
- VBA - Excel Macros
- VBA - Excel Terms
- VBA - Macro Comments
- VBA - Message Box
- VBA - Input Box
- VBA - Variables
- VBA - Constants
- VBA - Operators
- VBA - Decisions
- VBA - Loops
- VBA - Strings
- VBA - Date and Time
- VBA - Arrays
- VBA - Functions
- VBA - Sub Procedure
- VBA - Events
- VBA - Error Handling
- VBA - Excel Objects
- VBA - Text Files
- VBA - Programming Charts
- VBA - Userforms
- VBA Useful Resources
- VBA - Quick Guide
- VBA - Useful Resources
- VBA - Discussion
VBA - DatePart Function
A Function, which returns the specific part of the given date.
Syntax
DatePart(interval,date[,firstdayofweek[,firstweekofyear]])
Parameter Description
Interval − A required parameter. It can take the following values.
d - day of the year.
m - month of the year
y - year of the year
yyyy - year
w - weekday
ww - week
q - quarter
h - hour
n - minute
s - second
Date1 − A required parameter.
Firstdayofweek − An optional parameter. Specifies the first day of the week. It can take the following values.
0 = vbUseSystemDayOfWeek - Use National Language Support (NLS) API setting
1 = vbSunday - Sunday
2 = vbMonday - Monday
3 = vbTuesday - Tuesday
4 = vbWednesday - Wednesday
5 = vbThursday - Thursday
6 = vbFriday - Friday
7 = vbSaturday - Saturday
Firstdayofyear − An optional parameter. Specifies the first day of the year. It can take the following values.
0 = vbUseSystem - Use National Language Support (NLS) API setting
1 = vbFirstJan1 - Start with the week in which January 1 occurs (default)
2 = vbFirstFourDays - Start with the week that has at least four days in the new year
3 = vbFirstFullWeek - Start with the first full week of the new year
Example
Add a button and add the following function.
Private Sub Constant_demo_Click() Dim Quarter as Variant Dim DayOfYear as Variant Dim WeekOfYear as Variant Date1 = "2013-01-15" Quarter = DatePart("q", Date1) msgbox("Line 1 : " & Quarter) DayOfYear = DatePart("y", Date1) msgbox("Line 2 : " & DayOfYear) WeekOfYear = DatePart("ww", Date1) msgbox("Line 3 : " & WeekOfYear) msgbox("Line 4 : " & DatePart("m",Date1)) End Sub
When you execute the above function, it produces the following output.
Line 1 : 1 Line 2 : 15 Line 3 : 3 Line 4 : 1