Weekday excel vba excel

VBA Weekday function in Excel is categorized as a Date & Time function. This is a built-in Excel VBA Function. This function returns a number representing the day of the week, given a date value.

We can use this function in VBA as well as in Excel. This function we use in either procedure or function in a VBA editor window in Excel. We can use this VBA Weekday Function in any number of times in any number of procedures or functions. Let us learn what is the syntax and parameters of the Weekday function, where we can use this Weekday Function and real-time examples in Excel VBA.

Table of Contents:

  • Objective
  • Syntax of VBA Weekday Function
  • Parameters or Arguments
  • Where we can apply or use VBA Weekday Function?
  • Display the Weekday for specified date (default)
  • Display the Weekday for specified date using vbUseSystemDayOfWeek
  • Display the Weekday of date using vbSunday
  • Display the Weekday of date using vbMonday
  • Display the Weekday of date using vbTuesday
  • Display the Weekday of date using vbWednesday
  • Display the Weekday of date using vbThursday
  • Display the Weekday of date using vbFriday
  • Display the Weekday of date using vbSaturday
  • Instructions to Run VBA Macro Code
  • Other Useful Resources

The syntax of the Weekday Function in VBA is

Weekday(Date,[FirstDayOfWeek])

The Weekday function returns a numeric value. It can be any of the following value.

VB Constant Return Value
vbUseSystem Returns a number from 1 to 7 (It displays the first day of the week based on system settings).
vbSunday It returns a number from 1 to 7 (Monday to Sunday).
vbMonday It returns a number from 1 to 7 (Tuesday to Monday).
vbTuesday It returns a number from 1 to 7 (Wednesday to Tuesday).
vbWednesday It returns a number from 1 to 7 (Thursday to Wednesday).
vbThursday It returns a number from 1 to 7 (Friday to Thursday).
vbFriday It returns a number from 1 to 7 (Saturday to Friday).
vbSaturday It returns a number from 1 to 7 (Sunday to Saturday).

Parameters or Arguments:

The Weekday function has two arguments in Excel VBA.
where
Date:The Date is a required argument. It represents the date for which the weekday is to be returned.
[FirstDayOfWeek]:The [FirstDayOfWeek] is an optional argument. It represents the first day of the week. If ignored this parameter, vbSunday is the default value. It can be one of the following value.

VB Constant Value Description
vbUseSystem 0 Use the National Language Support(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

Where we can apply or use VBA Weekday Function?

We can use this Weekday Function in VBA MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.

Example 1: Display the Weekday for specified date

Here is a simple example of the VBA Weekday function. This below example displayed the Weekday of 01/01/2018.

'Display the Weekday for specified date
Sub VBA_Weekday_Function_Ex1()

    'Variable declaration
    Dim dDate As Date
    Dim iWkDay As Integer
                
    dDate = "01/01/2018"
    
    iWkDay = Weekday(dDate)
        
    MsgBox "The Weekday number of " & dDate & " is : " & iWkDay, vbInformation, "VBA Weekday Function"
    
End Sub

Output: Here is the screen shot of the first example output.
VBA Weekday Function

Example 2: Display the Weekday for specified date using vbUseSystemDayOfWeek

Let us see one more example of the VBA Weekday function. This below example displays the Weekday of 01/01/2018 using vbUseSystemDayOfWeek.

'Display the Weekday for specified date using vbUseSystemDayOfWeek  
Sub VBA_Weekday_Function_Ex2()

    'Variable declaration
    Dim dDate As Date
    Dim iWkDay As Integer
                
    dDate = "01/01/2018"
    
    iWkDay = Weekday(dDate, vbUseSystemDayOfWeek)
        
    MsgBox "The Weekday number of " & dDate & " using vbUseSystemDayOfWeek is : " & iWkDay, vbInformation, "VBA Weekday Function"
    
End Sub

Output:Here is the screen shot of the second example output.
VBA Weekday Function

Example 3: Display the Weekday for specified date using vbSunday

Let us see another example of the VBA Weekday function. This below example displays the Weekday of 01/01/2018 using vbSunday.

'Display the Weekday for specified date using vbSunday 
Sub VBA_Weekday_Function_Ex3()

    'Variable declaration
    Dim dDate As Date
    Dim iWkDay As Integer
                
    dDate = "01/01/2018"
    
    iWkDay = Weekday(dDate, vbSunday)
        
    MsgBox "The Weekday number of " & dDate & " using vbSunday is : " & iWkDay, vbInformation, "VBA Weekday Function"
    
End Sub

Output: Here is the screen shot of the third example output.
VBA Weekday Function

Example 4: Display the Weekday for specified date using vbMonday

One more example of the VBA Weekday function. This below example displays the Weekday of 01/01/2018 using vbMonday.

'Display the Weekday for specified date using vbMonday 
Sub VBA_Weekday_Function_Ex4()

    'Variable declaration
    Dim dDate As Date
    Dim iWkDay As Integer
                
    dDate = "01/01/2018"
    
    iWkDay = Weekday(dDate, vbMonday)
        
    MsgBox "The Weekday number of " & dDate & " using vbMonday is : " & iWkDay, vbInformation, "VBA Weekday Function"
    
End Sub

Output: Here is the screen shot of the fourth example output.
VBA Weekday Function

Example 5: Display the Weekday for specified date using vbTuesday

One more example of the VBA Weekday function. This below example displays the Weekday of 01/01/2018 using vbTuesday.

'Display the Weekday for specified date using vbTuesday  
Sub VBA_Weekday_Function_Ex5()

    'Variable declaration
    Dim dDate As Date
    Dim iWkDay As Integer
                
    dDate = "01/01/2018"
    
    iWkDay = Weekday(dDate, vbTuesday)
        
    MsgBox "The Weekday number of " & dDate & " using vbTuesday is : " & iWkDay, vbInformation, "VBA Weekday Function"
    
End Sub

Output: Here is the screen shot of the fifth example output.
VBA Weekday Function

Example 6: Display the Weekday for specified date using vbWednesday

Let us show another example of the VBA Weekday function. This below example displays the Weekday of 01/01/2018 using vbWednesday.

'Display the Weekday for specified date using vbWednesday
Sub VBA_Weekday_Function_Ex6()

    'Variable declaration
    Dim dDate As Date
    Dim iWkDay As Integer
                
    dDate = "01/01/2018"
    
    iWkDay = Weekday(dDate, vbWednesday)
        
    MsgBox "The Weekday number of " & dDate & " using vbWednesday is : " & iWkDay, vbInformation, "VBA Weekday Function"
    
End Sub

Output: Here is the screen shot of the sixth example output.
VBA Weekday Function

Example 7: Display the Weekday for specified date using vbThursday

Let us show one more example of the VBA Weekday function. This below example displays the Weekday of 01/01/2018 using vbThursday.

'Display the Weekday for specified date using vbThursday
Sub VBA_Weekday_Function_Ex7()

    'Variable declaration
    Dim dDate As Date
    Dim iWkDay As Integer
                
    dDate = "01/01/2018"
    
    iWkDay = Weekday(dDate, vbThursday)
        
    MsgBox "The Weekday number of " & dDate & " using vbThursday is : " & iWkDay, vbInformation, "VBA Weekday Function"
    
End Sub

Output: Here is the screen shot of the seventh example output.
VBA Weekday Function

Example 8: Display the Weekday for specified date using vbFriday

Let us show one more example of the VBA Weekday function. This below example displays the Weekday of 01/01/2018 using vbFriday.

'Display the Weekday for specified date using vbFriday
Sub VBA_Weekday_Function_Ex8()

    'Variable declaration
    Dim dDate As Date
    Dim iWkDay As Integer
                
    dDate = "01/01/2018"
    
    iWkDay = Weekday(dDate, vbFriday)
        
    MsgBox "The Weekday number of " & dDate & " using vbFriday is : " & iWkDay, vbInformation, "VBA Weekday Function"
    
End Sub

Output: Here is the screen shot of the eighth example output.
VBA Weekday Function

Example 9: Display the Weekday for specified date using vbSaturday

Let us show one more example of the VBA Weekday function. This below example displays the Weekday of 01/01/2018 using vbSaturday.

'Display the Weekday for specified date using vbSaturday
Sub VBA_Weekday_Function_Ex9()

    'Variable declaration
    Dim dDate As Date
    Dim iWkDay As Integer
                
    dDate = "01/01/2018"
    
    iWkDay = Weekday(dDate, vbSaturday)
        
    MsgBox "The Weekday number of " & dDate & " using vbSaturday is : " & iWkDay, vbInformation, "VBA Weekday Function"
    
End Sub

Output: Here is the screen shot of the ninth example output.
VBA Weekday Function

Instructions to Run VBA Macro Code or Procedure:

You can refer the following link for the step by step instructions.

Instructions to run VBA Macro Code

Other Useful Resources:

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

VBA Tutorial VBA Functions List VBA Arrays in Excel Blog

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers

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.

Table of contents
  • Excel VBA Weekday Function
    • What does Weekday Function do?
    • Examples
      • Example #1
        • Step 1: Define the variable as String
        • Step 2: Assign value to the variable
        • Step 3: Enter Date in Function
        • Step 4: Show Value of Variable in MsgBox
      • Example #2 – Arrive Whether the Date is on Weekend or Not
    • Recommended Articles

VBA Weekday

You are free to use this image on your website, templates, etc, Please provide us with an attribution linkArticle Link to be Hyperlinked
For eg:
Source: VBA Weekday (wallstreetmojo.com)

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.

VBA Weekday Formula

Date: For which date are we trying to find the weekday? It should be a proper date with the correct format.

[First Day of Week]: 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.

VBA Weekday Constant

Examples

You can download this VBA WeekDay Function Excel Template here – VBA WeekDay Function Excel Template

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

VBA Weekday Example 1

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

VBA Weekday Example 1-1

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

VBA Weekday Example 1-2

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 boxVBA MsgBox function is an output function which displays the generalized message provided by the developer. This statement has no arguments and the personalized messages in this function are written under the double quotes while for the values the variable reference is provided.read more.

Code:

Sub Weekday_Example1()

    Dim k As String

    k = Weekday("10-Apr-2019")

    MsgBox k

End Sub

Wday Example 1-3

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.

Note: My system’s starting day of the week is “Sunday.”

VBA Weekday example 1-4

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.

Wday Example 2

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

Wday Example 2-1

It will arrive at the results below.

VBA Weekday Example 2-2

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.

Recommended Articles

This article has been a guide to VBA Weekday. Here, we learn how to use the VBA Weekday function to get the last day of the week with examples and a downloadable Excel template. Below are some useful Excel articles related to VBA: –

  • IFERROR Function in VBA
  • VBA DateDiff Function
  • Excel Weekly Planner Template
  • What is VBA DATEVALUE Function?
  • VBA Paste Values

totn Excel Functions


This Excel tutorial explains how to use the Excel WEEKDAY function with syntax and examples.

Description

The Microsoft Excel WEEKDAY function returns a number representing the day of the week, given a date value.

The WEEKDAY function is a built-in function in Excel that is categorized as a Date/Time Function. It can be used as a worksheet function (WS) and a VBA function (VBA) in Excel. As a worksheet function, the WEEKDAY function can be entered as part of a formula in a cell of a worksheet. As a VBA function, you can use this function in macro code that is entered through the Microsoft Visual Basic Editor.

Syntax

The syntax for the WEEKDAY function in Microsoft Excel is:

WEEKDAY( serial_number, [return_value] )

Parameters or Arguments

serial_number
A date expressed as a serial number or a date in quotation marks.
return_value

Optional. It determines the day to use as the first day of the week in the calculations.

Warning: The return_value parameter accepts different values depending on whether you are using the WEEKDAY function as a worksheet function or a VBA function.

Worksheet Function

The return_value parameter, when used as a worksheet function, can be any of the following values:

Value Explanation Version
1 Returns a number from 1 (Sunday) to 7 (Saturday).
This is the default if parameter is omitted.
 
2 Returns a number from 1 (Monday) to 7 (Sunday).  
3 Returns a number from 0 (Monday) to 6 (Sunday).  
11 Returns a number from 1 (Monday) to 7 (Sunday). * Introduced in Excel 2010
12 Returns a number from 1 (Tuesday) to 7 (Monday). * Introduced in Excel 2010
13 Returns a number from 1 (Wednesday) to 7 (Tuesday). * Introduced in Excel 2010
14 Returns a number from 1 (Thursday) to 7 (Wednesday). * Introduced in Excel 2010
15 Returns a number from 1 (Friday) to 7 (Thursday). * Introduced in Excel 2010
16 Returns a number from 1 (Saturday) to 7 (Friday). * Introduced in Excel 2010
17 Returns a number from 1 (Sunday) to 7 (Saturday). * Introduced in Excel 2010

Note: Starting in Excel 2010, Microsoft has introduced new values for the return_value parameter when used as a worksheet function. You can now use 11 through 17 as valid parameters. This allows you to change the first day of the week to any day (Monday through Sunday).

VBA Function

The return_value parameter, when used as a VBA function, can be any of the following values:

Value Explanation
vbUseSystemDayOfWeek Returns a number from 1 to 7 and uses your system settings to determine the first day of the week
vbMonday Returns a number from 1 (Monday) to 7 (Sunday).
vbTuesday Returns a number from 1 (Tuesday) to 7 (Monday).
vbWednesday Returns a number from 1 (Wednesday) to 7 (Tuesday).
vbThursday Returns a number from 1 (Thursday) to 7 (Wednesday).
vbFriday Returns a number from 1 (Friday) to 7 (Thursday).
vbSaturday Returns a number from 1 (Saturday) to 7 (Friday).
vbSunday Returns a number from 1 (Sunday) to 7 (Saturday).

Returns

The WEEKDAY function returns a numeric value.

Applies To

  • Excel for Office 365, Excel 2019, Excel 2016, Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000

Type of Function

  • Worksheet function (WS)
  • VBA function (VBA)

Example (as Worksheet Function)

Let’s look at some Excel WEEKDAY function examples and explore how to use the WEEKDAY function as a worksheet function in Microsoft Excel:

Microsoft Excel

Based on the Excel spreadsheet above, the following WEEKDAY examples would return:

=WEEKDAY(A1)
Result: 1

=WEEKDAY(A1, 1)
Result: 1

=WEEKDAY(A1, 2)
Result: 7

=WEEKDAY(A2)
Result: 5

=WEEKDAY(A3)
Result: 6

=WEEKDAY(38157)
Result: 7

=WEEKDAY("Apr 21, 2015")
Result: 3

** Starting in Excel 2010, you can use 11 through 17 as the second parameter and change the first day of the week (in the calculations)

=WEEKDAY(DATE(2015,3,15),11)
Result: 7        (first day of the week is Monday)

=WEEKDAY(DATE(2015,3,15),12)
Result: 6        (first day of the week is Tuesday)

=WEEKDAY(DATE(2015,3,15),13)
Result: 5        (first day of the week is Wednesday)

=WEEKDAY(DATE(2015,3,15),14)
Result: 4        (first day of the week is Thursday)

=WEEKDAY(DATE(2015,3,15),15)
Result: 3        (first day of the week is Friday)

=WEEKDAY(DATE(2015,3,15),16)
Result: 2        (first day of the week is Saturday)

=WEEKDAY(DATE(2015,3,15),17)
Result: 1        (first day of the week is Sunday)

Example (as VBA Function)

The WEEKDAY function can also be used in VBA code in Microsoft Excel.

Let’s look at some Excel WEEKDAY function examples and explore how to use the WEEKDAY function in Excel VBA code:

Dim LWeekday As Integer

LWeekday = Weekday("12/31/2001", vbSunday)

In this example, the variable called LWeekday would now contain the value of 2.

Frequently Asked Questions

Question: Is there a LIKE function in Excel similar to the one in Access? I’m trying to write a formula equivalent to the following:

=if(D14 like "*Saturday*", Now()+2, Now()+1)

Where cell D14 is a date value formatted as Saturday, August 27, 2005.

Answer: Since your value in cell D14 is a date value, you can use the WEEKDAY function to determine which day of the week it is. In this case, you are looking for a Saturday. The WEEKDAY function will return a value of 7 when the date falls on a Saturday.

Try using the following formula:

=if(Weekday(D14)=7,Now()+2,Now()+1)

Return to VBA Code Examples

Weekday Description

Returns a number representing the day of the week, given a date value.

Simple Weekday Examples

Here is a simple Weekday example:

Sub Weekday_Example()
    MsgBox Weekday("1/1/2019")
End Sub

This code will return 3.(Because 1/1/2019 is Tuesday)

Weekday Syntax

In the VBA Editor, you can type  “Weekday(” to see the syntax for the Weekday Function:

The Weekday function contains 2 arguments:

Date: A Valid date (as number or string)

FirstDayOfWeek: [Optional] A constant that specifies the first day of the week. If not specified, vbSunday is assumed.

Examples of Excel VBA Weekday Function

MsgBox Weekday("1/1/2019", vbMonday)

Result: 2

MsgBox Weekday("1/1/2019", vbTuesday)

Result: 1

MsgBox Weekday("1/1/2019", vbFriday)

Result: 5

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro — A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!
vba save as

Learn More!

Home / VBA / Top VBA Functions / VBA WEEKDAY Function (Syntax + Example)

The VBA WEEKDAY function is listed under the date category of VBA functions. When you use it in a VBA code, it returns the day number (ranging from 1 to 7) by using the number of the day from the supplied date. In simple words, it returns the number of days within the week.

Weekday(Date, [FirstDayOfWeek])

Arguments

  • Date: A valid date for which you want to get the weekday.
  • [FirstDayOfWeek]: A string to define the first day of the week [This is an optional argument and if omitted VBA takes vbSunday by default].
    • vbUseSystemDayOfWeek – As per the system settings.
    • vbSunday – Sunday
    • vbMonday – Monday
    • vbTuesday – Tuesday
    • vbWednesday – Wednesday
    • vbThursday – Thursday
    • vbFriday – Friday

Example

To practically understand how to use the VBA WEEKDAY function, you need to go through the below example where we have written a vba code by using it:

Sub example_WEEKDAY()
Range("B1").Value = Weekday(Range("A1"))
End Sub

In the above code, we have used WEEKDAY to get the weekday for (Wednesday, May 15, 2019) the date which we have in cell A1 and in the result, we have 4 in cell B1.

The day is Wednesday, which is the fourth day of the week (it has taken Sunday the first day of the week), so it has returned 4 in the result.

Notes

  • If the date specified is a value other than a date or a string that can’t be recognized as a date, VBA will return the run-time 13 error.

Понравилась статья? Поделить с друзьями:
  • Week numbering in excel
  • Week meaning of the word
  • Week by week excel template
  • Week after next word
  • Week 1 your word day 1