×

Date and Time in Excel-VBA

In this modern world of computer technology the creation of Excel reports are termed to be the crucial elements, and the particular type of programs that helps us in performing of the tasks is none other than the Microsoft Excel.

Besides all these, there is the availability of the other most essential tool to achieve these various tasks is VBA, which is the acronym for the Virtual Basic for Applications. This application is termed the Microsoft Applications, whose practical knowledge plays an essential role for the professionals in efficiently maintaining or handling the massive amount of available data. Due to this, the corporate world's people must have training related to macro excel.

This tutorial we will quickly cover the various steps involved in adding data and time in Excel VBA.

How to Add DATE and TIME in the Microsoft Excel- VBA?

Before discussing the topics of how to work with the data and the Time in the Microsoft Excel –VBA, let us first know about the following things in detail as follows:

  1. What do you understand by the term Excel VBA?
  2. How to open VBA in Excel 2010?

Now, moving on to discussing each of the above concepts one after the other in detail,

What do you understand by the term Excel VBA?

The word VBA is an acronym for Visual Basic for Applications. And the term Excel VBA is defined to be the most acceptable type of Microsoft's programming language that can be used for Excel and the other various Microsoft office programs; some of them are as follows:

  • Microsoft Word.
  • Microsoft PowerPoint.

And it was noted that the particular Office suite programs share a standard programming language.

How to open VBA in Excel 2010?

Opening of the VBA Environment

An individual can easily access the VBA (Virtual Basic for Applications) environment in Microsoft Excel 2010 with the help of the Microsoft Visual Basic for Applications window.

One must ensure that the Developer tab is visible in the toolbar. As seen in the attached Screenshot.

Date and Time in Excel-VBA

And the Developer tab is a toolbar which has specific types of buttons for the opening of the VBA editor and to make the creation of the Form/ActiveX controls, such as the checkboxes, buttons and many more.

  • If the Developer tab is unavailable over the toolbar, one can follow the following steps to make it appear on the taskbar.
  • Click on the File menu option from the toolbar, and we will display the various options after clicking on it.
Date and Time in Excel-VBA
  • And in that options, click on the Options buttons. An excel options window will appear, and one should Customize Ribbon options on the left-hand side.
Date and Time in Excel-VBA
  • After choosing the Customize Ribbon option, a window with lots of opportunities will open, and one should click on the Developer checkbox on the right-hand side.
Date and Time in Excel-VBA
  • After that, click on the Ok button.
Date and Time in Excel-VBA
  • And the Developer tab will get appeared in the toolbar effectively.
  • Now one should select the Developer tab from the toolbar and then click or select the Visual Basic option in the Code Group.
Date and Time in Excel-VBA

And now, doing the above steps, one can get the Microsoft Visual Basic Editor on their screen and write the specific amount of code for performing the various operations.

Date and Time in Excel-VBA

How to make changes in the format of the Date in VBA?

Let us understand this with the help of the example discussed below:

# Example 1:

Below given is an example wherein the excel sheet has multiple cells occupied with the same date.

Date and Time in Excel-VBA

And in these, we will perform or apply the various data formats for the same data to visualize its impact.

And to do this, we will firstly be copying the data to the respective next column,

Date and Time in Excel-VBA

And now, we will first apply the format of “DD-MM-YYYY” FORMAT in the respective cell called A1.

Date and Time in Excel-VBA

Now in this code, we will select the cell with the help of the RANGE object.

Code:

Sub Date_Format_Example1 ()
Range (“A1”)
End Sub

Code written in the VBA (Virtual Basic for Applications), and screenshots is attached below.

Date and Time in Excel-VBA

As we are making changes in the date format of the individual cell, we are liable to access the “Number Format” property from the object RANGE. And then, we need to set the number format by putting the equality sign in the double quotations.

Code:

Sub Date_Format_Example1 ()
Range (“A1”).NumberFormat =”dd-mm-yy”
'This will change the Date to 24/10/2020'
End Sub
Date and Time in Excel-VBA

And the output for the above results is as follows:

Date and Time in Excel-VBA

# Example 2:

Similarly, we are using different formatting codes according to our needs and the choice of the other cells. We have also mentioned the regulations and codes on the VBA and their Screenshot.

Code:

Sub Date_Format_Example2 ()
Range (“A2”) .NumberFormat =”ddd-mm-yyy”
'This will change the Date to "Sat-10-2020."
End Sub

End Sub

Date and Time in Excel-VBA

Output:

The output for the above code is shown below in the screenshots:

Date and Time in Excel-VBA
Sub Date_Format_Example2 ()
Range (“A3”) .NumberFormat =”dddd-mm-yyy”
'This will change the Date to "Saturday-10-2020."
End Sub

Code written in the VBA (Virtual Basic for Applications), and screenshots is attached below.

Date and Time in Excel-VBA

Output:

 The output for the above code is shown below in the screenshots:

Date and Time in Excel-VBA

Change Date Format with the help of using the FORMAT Function

It was known that in the respective VBA (Virtual Basic for Applications), we have a separate Function known as the FORMAT function that the particular individuals can use to the specific format of the cell.

Syntax:

An individual must specify the value used for the "Expression" and apply the particular "FORMAT" accordingly.

Code:

Sub Date_Format_Example ()
Dim MyVal As a Variant
Val =58967
MsgBox Format(MyVal, “DD-MM-YYYY”)
End Sub
Date and Time in Excel-VBA

Output:

 The output for the above code is shown below in the screenshots:

Date and Time in Excel-VBA

Following points to remember

The following points that an individual must remember while working with Microsoft Excel using the VBA ((Virtual Basic for Applications) are as follows:

  • The critical point is that an individual system's default date will be effectively applied to excel.
  • And the particular type of the Number Format property can be used by an individual to change the format of the respective date in the VBA (Virtual Basic for Applications).
  • And by the help of the Format Function, one can change the format of the date as per the needs of the individual and their choice.

How to make use of the Time Function in the Excel VBA

The particular VBA (Virtual Basic for Applications) returns the current Time. The essential point that needs to be noted by an individual is that the above-used function does not have any arguments, which means a process with no ideas. The other important point is that the above-used function returns the current Time of the respective computer system.

And with the help of the VBA (Virtual Basic for Applications), one can easily find out the actual Time that is usually taken by the particular lines of the codes to complete the process efficiently.

Besides all this, Time is that kind of function (volatile function) with no syntax.

And in these modern worlds of computer science, excel also provides a similar function that is the NOW () function, which in turn is used for inserting the current times and the current date in the working area of the excel sheet called the spreadsheet.

TIME ()

In this, an individual needs to enter the function, and there is no need for the parenthesis to enclose it, as TIME is capable enough of inserting the current Time, and the results given by the TIME function are in the form of the strings.

How to use the TIME Function in the VBA (Virtual Basic for Applications)?

Now, the understanding of the Time Function in excel is as follows:

One can make use of the Time Function by following up on the below-mentioned steps:

STEP 1: First, an individual needs to create a macro, and the code for that macro is as follows:

Code:

Sub Time-Example ()
End Sub

Code written in the VBA (Virtual Basic for Applications), and screenshots is attached below.

Date and Time in Excel-VBA

STEP 2:  Declaring the variable in the form of the string and the respective code for that is as follows with the help of the attached screenshots on the VBA.

Sub Time_Example()
Dim CurrentTime As String
End Sub

Code written in the VBA (Virtual Basic for Applications), and screenshots is attached below.

Date and Time in Excel-VBA

STEP 3: After that, one should assign the value to this variable through the TIME Function, and the associated code on the VBA with the Screenshot is as follows:

Sub Time_Example()
Dim CurrentTime As String
CurrentTime=Time
End Sub

Code written in the VBA (Virtual Basic for Applications), and screenshots is attached below.

Date and Time in Excel-VBA

STEP 4: Now, we will display the results in the message box

Code:

Sub Time_Example()
Dim CurrentTime As String
CurrentTime=Time
MsgBox CurrentTime=Time
End Sub
Date and Time in Excel-VBA

And the output for the above code is as follows:

Date and Time in Excel-VBA

In this way, by following the steps mentioned earlier, one can effectively insert the current Time in the excel sheet according to their need of use.


Related Topics

What do you understand by Combination Chart in Microsoft Excel?

In Microsoft Excel, an individual has the Combo Chart option available, which can be effectively clubbed into two charts types, which are Column Clusters Chart, Line Chart to get the...

7 minutes read.

Spell Check in Excel

Spell Check in Excel Microsoft Excel facilities the complete features for examining the work with text. It provides the basic properties for the proper functioning of text, including the spell-check property. The...

5 minutes read.

Excel IFNA Function

Excel IFNA Function The IFNA in Excel is used to remove the #N/A type of errors in excel values cells with our choice of value. IFNA function is from Excel 2013 onwards, so it...

1 minute read.

Data Validation in Excel

What is Data Validation? Data Validation is one of the features in Excel that allows user to restrict values which other people can fill in – for example, in a form, you may...

4 minutes read.

What is a name box in Excel?

The name box in Microsoft Excel specifies a name for a single cell or a table. The name box is present in the top left corner, left to the formula...

4 minutes read.

Excel IFERROR() Function

The IFERROR() function in excel returns the specified value if initially supplied the argument or formula returns an error, otherwise returns the result of the formula supplied in the first argument. When...

2 minutes read.

Count Characters in Excel

Microsoft Excel is used to perform calculations for multiple purposes. The data entered in the worksheet is a combination of numeric and alphabets. Sometimes there is a need to check...

4 minutes read.

Calculating the Last Day of the Month in Excel

In Microsoft Excel, various functions exist to calculate the date and time from the current date to the past and future. Among multiple tasks in this tutorial, let's see how...

3 minutes read.

Absolute Value in Excel

What is Absolute Value in Excel? Microsoft Excel worksheet is a combination of numeric values and alphabets. The numeric value is either a positive value or a negative value. Sometimes the...

5 minutes read.

Excel File using Password

How to Protect an Excel File using Password? Nowadays Microsoft Excel is widely used by large organizations to small organizations for their personal and professional usage. They share files to various...

2 minutes read.

Excel COUNTBLANK() Function

Excel COUNTBLANK() Function The COUNTBLANK() function in excel counts the number of blank cells within a range. Syntax COUNTBLANK ([value1], [value [2], ...) Parameter value1(required)- This parameter represents the first cell in the range. value2, …(optional)- It represents...

1 minute read.

How to generate random numbers in Excel

In this tutorial, we will discuss the following things in detail that are as follows: Introduction about Random Numbers used in Microsoft Excel. Discuss how to generate the Random Numbers in Microsoft...

6 minutes read.

Averageif Function in Excel

Average If in HTML In Mathematics, Average function is used to find the arithmetic mean of the given data. It is defined as the dividing the sum total of given data...

7 minutes read.

Notes in Excel

Notes in Excel Notes are used to include additional information to the cell and explain formulas calculation, any modification made to the cell, providing necessary information on data that is entered or to provide any...

3 minutes read.

Excel HOUR() Function

Excel HOUR() Function The HOUR() function in excel converts a serial number to an hour (0 - 23). Syntax HOUR(SERIAL_NUMBER) Parameter SERIAL_NUMBER (required)- This parameter represents the date or time from which you want to extract...

1 minute read.

Check Marks in Excel

What is a Check Mark? Among various characters checkmark is one of the characters used to indicate that the item or product in the list is correct, chosen or selected. A...

5 minutes read.

Application Interface and Key Components

The essential key components of Excel are as follows: Ribbon TabQuick Access ToolbarFormula BarActive CellColumnsRows Name BoxSheet TabsZoom ControlStatus Bar Ribbon Tab: The ribbon includes tabs labeled Home, Insert, Page Layout, Formulas, Data, Review,...

3 minutes read.

Excel LARGE() Function

The LARGE() function in excel returns the k-th largest value in a data set. Syntax LARGE (Array, kth small number from the list) Parameter Array (required)- This parameter represents the list of numbers from which nth...

1 minute read.

Excel MID() Function

The MID() function in excel is used to return a specific number of characters from a text string starting at the position you specify. One can change the start of the string, thus...

1 minute read.

Excel LEFT() Function

The LEFT () function in excel returns the leftmost characters from a text value. This function is used for data manipulation. Syntax LEFT(Text, [num_chars]) Parameter Text (required)- This parameter represents the string from which you want to...

1 minute read.