Table Of Contents
Excel VBA Weekday Function
Weekday in VBA is a date and time function that one may use to identify the weekday of a given date provided as input. This function returns an integer value from 1 to 7 range. An optional argument is provided to this function, which is the first day of the week. But, if we do not provide the first day of the week, the function assumes Sunday as the first day of the week by default.
Can we tell the weekday number by looking at a particular date? Yes, we can tell the day number that week, depending upon the starting day of the week. In regular worksheet functions, we have a function called WEEKDAY in excel to tell the week's number for a particular date. In VBA, too, we have the same function to find the same thing.
What does Weekday Function do?
The Weekday function returns the provided date's day number in the week. So, for example, if you have dates 01st April to 07th April and if you want to know the day of the date 05th April if the starting day of the week is from Monday, it is the 5th day.
To find this, we have the same function as "Weekday" in a worksheet and VBA. Below is the syntax of the function.
Date: For which date are we trying to find the weekday? It should be a proper date with the correct format.
: To determine the weekday of the provided Date, we need to mention what is the first day of the week. By default, VBA considers "Monday" as the starting day of the week. Apart from this, we can supply the below days as well.
Examples
Example #1
To start the proceedings, let me start with a simple example first up. Now we will try to find the weekday for the date "10-April-2019."
Step 1: Define the variable as String
Code:
Sub Weekday_Example1() Dim k As String End Sub
Step 2: Assign value to the variable
Assign the value to the variable "k" by applying the WEEKDAY function.
Code:
Sub Weekday_Example1() Dim k As String k = Weekday( End Sub
Step 3: Enter Date in Function
The date we are testing here is "10-Apr-2019", so pass the date as "10-Apr-2019."
Code:
Sub Weekday_Example1() Dim k As String k = Weekday("10-Apr-2019" End Sub
Step 4: Show Value of Variable in MsgBox
By default, it takes the first day of the week as "Monday," so ignore this part. Close the bracket. The next line shows the variable "k" value in the VBA message box.
Code:
Sub Weekday_Example1() Dim k As String k = Weekday("10-Apr-2019") MsgBox k End Sub
We have completed it now.
If we run the code, we will get the result as "4" because starting from Sunday, the provided date (10-Apr-2019) falls on the 4th day of the week.
Similarly, if you change the start day of the week, it keeps varying. Below is an example line for the same.
Code:
k = Weekday("10-Apr-2019", vbMonday) ‘This returns 3 k = Weekday("10-Apr-2019", vbTuesday) ‘This returns 2 k = Weekday("10-Apr-2019", vbWednesday) ‘This returns 1 k = Weekday("10-Apr-2019", vbThursday) ‘This returns 7 k = Weekday("10-Apr-2019", vbFriday) ‘This returns 6 k = Weekday("10-Apr-2019", vbSaturday) ‘This returns 5 k = Weekday("10-Apr-2019", vbSunday) ‘This returns 4
Example #2 - Arrive Whether the Date is on Weekend or Not
Assume you have a date like the one below. You want to find the date next weekend. Then, we can use the WEEKDAY function to arrive at the results.
We need to use WEEKDAY with IF condition and loops to arrive at the result. We have written the code for you to go line by line to get the logic.
Code:
Sub Weekend_Dates() Dim k As Integer For k = 2 To 9 If Weekday(Cells(k, 1).Value, vbMonday) = 1 Then Cells(k, 2).Value = Cells(k, 1) + 5 ElseIf Weekday(Cells(k, 1).Value, vbMonday) = 2 Then Cells(k, 2).Value = Cells(k, 1) + 4 ElseIf Weekday(Cells(k, 1).Value, vbMonday) = 3 Then Cells(k, 2).Value = Cells(k, 1) + 3 ElseIf Weekday(Cells(k, 1).Value, vbMonday) = 4 Then Cells(k, 2).Value = Cells(k, 1) + 2 ElseIf Weekday(Cells(k, 1).Value, vbMonday) = 5 Then Cells(k, 2).Value = Cells(k, 1) + 1 Else Cells(k, 2).Value = "This is actually the weekend Date" End If Next k End Sub
It will arrive at the results below.
Look at cells B6 and B7. We got the result as "This is actually the weekend date" because dates "04-May-2019" and "06-Apr-2019" are weekend dates, so there is no need to show the weekend date for weekend dates. By default, we get the result as this.