What is VBA WEEKDAY Function
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 the day within the week.
how to use it
To use VBA's WEEKDAY function you need to understand its syntax and 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 use WEEKDAY Function in VBA
To practically understand how to use 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 which we have in the cell A1 and in the result, we have 4 in the cell B1. The day is Wednesday, which is the fourth day of the week (it has taken Sunday the first day of the week), so that it has returned 4 in the result.
Below are some important points which you need to take care while using WEEKDAY function in VBA.
- 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.
About the Author
Puneet is using Excel since his college days. He helped thousands of people to understand the power of the spreadsheets and learn Microsoft Excel. You can ﬁnd him online, tweeting about Excel, on a running track, or sometimes hiking up a mountain.