TOP

VBA Function: Weekday

Description

The VBA Weekday function returns the number of the day of the week (from 1 to 7) for the given date.


Weekday syntax

Weekday(date)

Or

Weekday(date, first_day_of_week)

Numbering

By default, the first day of the week is Sunday (1) and the last day of the week is Saturday (7).

To renumber so that Monday is the first day of the week (1) and Sunday is the last (7), add the value 2 as the second argument when using this function:

Weekday(date, 2)

VBA Weekday Example

Using the Weekday function to display the number of the day of the week for multiple dates:

sub example()
    
      MsgBox Weekday(#11/2/2020#, 2) 'Returns: 1
      MsgBox Weekday("3.11.20", 2) 'Returns: 2
      MsgBox Weekday("4 nov 2020", 2) 'Returns: 3
      MsgBox Weekday("5/11/2020 17:30:21", 2) 'Returns: 4
    
end sub

Using the Weekday function to distinguish between a weekday and a weekend:

sub example()

      If Weekday(Now, 2) < 6 Then
          MsgBox "It's a weekday..."
      Else
          MsgBox "It's the weekend!"
      End if
    
end sub
Function that returns the name of the day of the week: WEEKDAYNAME.