The VBA WEEKDAY function is listed under the date category of VBA functions. When you use it in a VBA code, it returns the day number (ranging from 1 to 7) by using the number of the day from the supplied date. In simple words, it returns the number of days within the week.
Syntax
Weekday(Date, [FirstDayOfWeek])
Arguments
- Date: A valid date for which you want to get the weekday.
- [FirstDayOfWeek]: A string to define the first day of the week [This is an optional argument and if omitted VBA takes vbSunday by default].
- vbUseSystemDayOfWeek – As per the system settings.
- vbSunday – Sunday
- vbMonday – Monday
- vbTuesday – Tuesday
- vbWednesday – Wednesday
- vbThursday – Thursday
- vbFriday – Friday
Example
To practically understand how to use the VBA WEEKDAY function, you need to go through the below example where we have written a vba code by using it:
Sub example_WEEKDAY()
Range("B1").Value = Weekday(Range("A1"))
End Sub
In the above code, we have used WEEKDAY to get the weekday for (Wednesday, May 15, 2019) the date we have in cell A1, and the result, we have 4 in cell B1.
The day is Wednesday, the fourth day of the week (it took Sunday as the first day), so it has returned 4 in the result.
Notes
- If the date specified is a value other than a date or a string that can’t be recognized as a date, VBA will return the run-time 13 error.
- Back to the Excel VBA / VBA Functions