VBA DAY Function

VBA DAY Function in Excel

VBA Day Function in Excel is a built-in function in MS Excel. It has one input argument or parameter. It returns the two-digit Day from the specified date and returns a number from 1 to 31. VBA Day Function is a ‘Data and Day’ type function. Default format of the Day function is ‘MM’. The VBA Day Function can be used in either procedure or function in a VBA editor window in Excel. We can use the VBA Day Function any number of Days in any number of procedures or functions. In the following section we learn what is the syntax and parameters of the date function, where we can use this VBA Day Function and real-Day examples.

Syntax of VBA Day Function

The syntax of the VBA Day Function is

DAY(date_value)

Parameters or Arguments

There is one parameter or argument for the Day function in VBA. That is nothing but ‘date_value’.
Where date_value is a date for which the Day (a number from 1 to 31) is to be returned.

Where we can apply or use the VBA Day Function?

We can use this VBA Day function in 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 current Day on the screen

Here is a simple example of the VBA Day Function. This below example macro uses the Day function and displays the two-digit Day from the specified date. In this case specified date is current date.

'Display Current Day on the screen
Sub VBA_Day_Function_Ex1()
    
    'Variable declaration
    Dim sCurrentDay As Integer
    
    'Assign current Day to variable
    sCurrentDay = Day(Now)
    MsgBox sCurrentDay, vbInformation, "Current Day"
    
End Sub 

In the above example ‘sCurrentDate’ declared as an Integer data type. And Day is a vba function, it returns two digit number (number from a 1 to 31) as Day. This variable ‘sCurrentDate’ now contains the two-digit Day.
Output: Here is the screen shot of first example output.
VBA Day Function Example 1

Example 2: Display current Day on the Worksheet

Here is another example of the VBA Day Function. This below example macro uses the Day function and displays the Day from the specified date on the Worksheet named ‘VBAF1.Com’ in Range ‘B18’.

Sub VBA_Day_Function_Ex2()
'Display Current Day on the Worksheet
   
    'Variable declaration
    Dim dCurrentDay As Date
    
    'Assign current Day to variable
    sCurrentDay = Now
    Sheets("VBAF1.com").Range("B18") = Day(sCurrentDay)
    
    'or
    'Sheets("VBAF1.com").Cells(18, 2) = Day(sCurrentDay)
    
End Sub
    

Output:Here is the screen shot of second example output.
VBA Day Function Example 2

 

Example 3: Display complete current Day name using Format

Here is one more example with VBA Day Function. This below example macro uses the Date function and changes the format of the date. Finally, it displays the complete current Day name from the specified date on the screen.

Sub VBA_Day_Function_Ex3()
'Format and Display complete Current Day on the screen
    
    'Variable declaration
    Dim sCurrentDay As Date
    
    'Assign current Day to variable
    sCurrentDay = Date
    MsgBox Format(sCurrentDay, "DDDD"), vbInformation, "Current Day"
    
End Sub

Output:Here is the screen shot of third example output.
VBA Day Function Example 3

Download File

Click on following link to download free example excel workbook to learn more about the VBA Day function.

VBA Day Function Examples

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

Leave a Reply