Excel/VBA Weekday Function

The Weekday function returns the number of day of the Week for the given date.

What is Weekday function in VBA?

The Weekday function returns the number of day of the Week for the given date.

For example, we want to know what is Weekday (Sat, Sun, Mon, etc.) for the date 1st Jan 2000.

We can pass this date to the Weekday() function and it returns the number of day as a whole number.

Syntax:

Weekday(date, [ firstdayofweek ])

The last section of tutorial shows the usage of firstdayofweek argument.

An example of using Weekday function in VBA

For the example, we are using the same date as given above i.e. 1st Jan 2001.

See what we get after using the Weekday function and displaying the returned value in a message box:

Output:

VBA-weekday

How VBA treats the number of day?

Well, VBA has meaning for the return values as follows:

  • Constant Value    Description
  • vbSunday 1              Sunday
  • vbMonday 2              Monday
  • vbTuesday 3              Tuesday
  • vbWednesday 4              Wednesday
  • vbThursday 5              Thursday
  • vbFriday 6              Friday
  • vbSaturday 7              Saturday

As we got 7 in our above example, so it was Saturday.

Using Select/Case statement in VBA to get day name for day number

In the following example, we are using VBA Select Case statement, so that a day name is displayed based on the given date.

As such, it may be hard to remember which number belongs to what day name.

Code:

Output:

VBA-weekday-case

Using second argument: firstdayofweek

If you want to change the first day of the week other than Sunday (default) then you may specify it by using the firstdayofweek argument.

It has the following possible values:

  • vbUseSystem 0              Use the NLS API setting.
  • vbSunday 1              Sunday (default)
  • vbMonday 2              Monday
  • vbTuesday 3              Tuesday
  • vbWednesday 4              Wednesday
  • vbThursday 5              Thursday
  • vbFriday 6              Friday
  • vbSaturday 7              Saturday

In the example below, we will make Monday as the first day of the Week.

Code:

Output:

VBA-weekday-firstday

You can see, we used the same code as in the first example except used the second argument and we got 6 for Saturday unlike 7 in the first example.

Using Excel cells to get the number of day in the week

To make things more clear about Weekday function and particularly, its second parameter, we will use a range of cells from A2 to A6 in an Excel sheet.

We have different date formats in the cells.

In VBA code, we used a For..Each loop. In each iteration, the Weekday() function is used to get the number of day.

The returned result is assigned to adjacent B, C, and D cells.

  • B2:B6 = Sunday as 1
  • C2:C6 = Monday as 1
  • D2:D6 = Friday as 1

See the code and output below:

Output:

VBA-weekday-Range-Excel