How to use the VBA WEEKDAY Function (Syntax + Example)

HomeVBA Functions LIST (Category Wise)How to use the VBA WEEKDAY Function (Syntax + Example)

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.

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 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.

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.

Related Functions

DATE | DATEADD | DATEDIFF | DATEPART | DATESERIAL | DATEVALUE | DAY | MONTH | MONTHNAME | WEEKDAYNAME | YEAR