Excel VBA Weekday Function
Excel VBA Weekday Function: The TimeValue function in VBA returns an integer (1 to 7), signifying the day of the week for the specified date.
Syntax
Weekday (Date, [FirstDayOfWeek])
Parameter
Date (required) – This parameter represents a valid String interpretation of time.
FirstDayOfWeek (optional) – This parameter specifies the weekday that should be used as the first day of the week. The default value is set to vbUseSystemDayOfWeek.
It can take the following values:
- vbUseSystemDayOfWeek - The first day of the week is as specified in your system settings
- vbSunday – Sunday
- vbMonday – Monday
- vbTuesday – Tuesday
- vbWednesday – Wednesday
- vbThursday – Thursday
- vbFriday – Friday
- vbSaturday – Saturday
Return
This function returns an integer (1 to 7), signifying the day of the week for the specified date.
Example 1
Sub WeekdayFunction_Example1() 'It will return the weekday for the given date Dim wekday_val As Integer wekday_val = weekday(#2/11/2020#) ' The variable wekday_val now equals 03. Cells(1, 1).Value = wekday_val End Sub
Output
3
Example 2
Sub WeekdayFunction_Example2() 'It will return the weekday for the given date Dim wekday_val As Integer 'the week will start with Monday wekday_val = weekday(#2/11/2020#, vbMonday) ' The variable wekday_val now equals 02. Cells(1, 1).Value = wekday_val End Sub
Output
2
Example 3
Sub WeekdayFunction_Example3() 'It will return the weekday for the given date Dim wekday_val As Integer 'the week will start with Thursday wekday_val = weekday(#12/31/2020#, vbThursday) ' The variable wekday_val now equals 1. Cells(1, 1).Value = wekday_val End Sub
Output
1
Example 4
Sub WeekdayFunction_Example4() ' It will return the weekday for the date Dim wekday_val As String 'will returns today's weekday wekday_val = WeekdayName(weekday(Now())) ' The variable wekday_val will return the string "Tuesday". Cells(1, 1).Value = wekday_val End Sub
Output
Tuesday