
In VBA (Visual Basic for Applications), the `WeekdayName` function is used to retrieve the name of the day of the week corresponding to a specific weekday number. The `WeekdayName` function syntax is as follows:
WeekdayName(weekday, [abbreviate], [firstdayofweek])
Where:
- `weekday` is a required parameter that is the day of the week number (1 for Sunday, 2 for Monday, …, 7 for Saturday).
- `abbreviate` is an optional Boolean value that indicates if the weekday name should be abbreviated (`True`) or not (`False`). If omitted, the default is the full weekday name.
- `firstdayofweek` is an optional parameter that specifies the first day of the week. You can use the VBA constants (e.g., `vbSunday`, `vbMonday`, etc.) to set this. If this parameter is omitted, `vbSunday` (1) is assumed.
Here’s an example demonstrating how to use the `WeekdayName` function:
Sub ExampleWeekdayName()
Dim weekdayNum As Integer
Dim myWeekdayName As String
' Assign a number to represent a day of the week (1 = Sunday, 2 = Monday, etc.)
weekdayNum = 3 ' This represents Tuesday
' Get the full name of the weekday (unabbreviated)
myWeekdayName = WeekdayName(weekdayNum)
Debug.Print "The full name of day " & weekdayNum & " is " & myWeekdayName
' Get the abbreviated name of the weekday
myWeekdayName = WeekdayName(weekdayNum, True)
Debug.Print "The abbreviated name of day " & weekdayNum & " is " & myWeekdayName
' Get the name of the weekday, considering Monday as the first day of the week
myWeekdayName = WeekdayName(weekdayNum, False, vbMonday)
Debug.Print "Considering Monday as the first day of the week, day " & weekdayNum & " is " & myWeekdayName
End Sub
When you run this subroutine (`Sub`), it will print to the immediate window the full weekday name corresponding to the weekday number 3, the abbreviated weekday name, and the name taking into account that the first day of the week is Monday.
If you want to use the `WeekdayName` function within another subroutine or function, simply call it with the appropriate parameters and use the result as needed within your code.