How do I use the WeekdayName function in VBA, and can you provide an example?

The WeekdayName function in VBA is used to return the name of the weekday based on a given date. It takes in two parameters – the first being the weekday number (1 for Sunday, 2 for Monday, etc.), and the second being an optional boolean value indicating whether the weekday name should be abbreviated or not. An example of using this function would be: WeekdayName(3, False) which would return the full name of the third weekday, which is Tuesday. This function is particularly useful when working with large datasets and organizing data by weekday.

Use WeekdayName Function in VBA (With Example)


You can use the WeekdayName function in VBA to obtain the name of the day of the week from a given date.

Here is one common way to use this function in practice:

Sub FindWeekdayName()
    
Dim i As Integer

For i = 2 To 9
    Range("B" & i) = WeekdayName(Weekday(Range("A" & i)))
Next i
    
End Sub

This particular macro will find the name of the weekday for each date in the range A2:A9 and display the results in the range B2:B9.

The following example shows how to use the WeekdayName function in practice.

Note: If you would rather return the day of the week as an integer then you should use the function instead.

Example: How to Use WeekdayName Function in VBA

Suppose we have the following column of dates in Excel:

Suppose we would like to obtain the name of the weekday for each date and display the results in column B.

We can create the following macro to do so:

Sub FindWeekdayName()
    
Dim i As Integer

For i = 2 To 9
    Range("B" & i) = WeekdayName(Weekday(Range("A" & i)))
Next i
    
End Sub

When we run this macro, we receive the following output:

Column B displays the name of the weekday for each date in column A.

For example:

  • 1/1/2023 is on a Sunday.
  • 1/4/2023 is on a Wednesday.
  • 2/23/2023 is on a Thursday.

Note: You can find the complete documentation for the VBA WeekdayName function .

Additional Resources

The following tutorials explain how to perform other common tasks in VBA:

x