×

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

Shortcut Keys in Excel

Shortcut Keys in Excel Shortcut keys in Excel are used to simplify the work and save time by pressing a few keys.  These keys are used for navigating and working with Excel spreadsheets quickly and easily. Basic Selection:...

9 minutes 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.

How to apply Data Protection to the Worksheet?

What do we mean by Data Protection? Data Protection in excel involves in protecting your file/data from some other user and also prevents the following accidents – Accidentally deleting or changing the formulas in...

3 minutes read.

Converting Units in Excel

The numeric values present in Excel consist of various types such as constant, integer, decimal etc. For calculation purposes, the values present in the data need to be converted to...

4 minutes read.

Excel SUBSTITUTE() Function

The SUBSTITUTE() function in excel substitutes/replaces the old text with the new text in a string. SUBSTITUTE function is used to clean data. This function is case-sensitive. Syntax SUBSTITUTE(Text, Old_Text, New_Text, [instance_number]) Parameter Text (required)- This...

2 minutes read.

Trendline in Excel

A Trendline is a line imposed on a graph to predict the direction of the data. It connects the series of data together to showcase the data best fit. The...

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.

AutoFill in Excel

AutoFill in Excel Excel is known for its credibility to operate repeated and similar tasks automatically. For this purpose, it facilitates an inbuilt feature known as Autofill that recognizes the series of...

3 minutes read.

Conditional Formatting in Excel

Conditional Formatting What are the conditional formats? Conditional Formatting is a tool that allows the user to format cells or range of cells based on the selected condition or given criteria. The formatting will...

4 minutes read.

How to Highlight Duplicates Words in the Microsoft Excel?

We can easily highlight the values that are duplicated in the selected dataset, whether it could be a column or a row from the particular table, with the help of...

6 minutes read.

Excel WEEKNUM() Function

The WEEKNUM() function in excel returns the week number from 1 to 54 of a specific date in a year. The WEEKNUM function starts counting with the week that contains January 1...

1 minute read.

How to create drop down in excel?

How to create drop down in excel? Drop-down lists are mostly used to facilitate data entry operations. They are commonly used in interactive websites or applications. Microsoft has expanded his Excel's data validation feature...

5 minutes read.

Excel COUNT() Function

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

1 minute read.

SumIF Formula in Microsoft Excel

SUMIF Formula in Microsoft Excel is considered the primary and vital formula individuals can use to remove their workload. In this tutorial, we will discuss and learn about the SUMIF Formula...

7 minutes read.

How to make use of the Wildcard in Excel

In this modern world, it is known that the wildcard characters in Microsoft Excel are considered to be the most essential and unrated features of Microsoft Excel. Besides all these,...

8 minutes read.

How to make use of the Excel Autofit in Excel

In Microsoft Excel, Autofit can be used to adjust and then fix the column and the respective row height to till the maximum limit until and unless it has the...

7 minutes read.

Custom Sort Order in Excel

Microsoft Excel Worksheet is widely used for office, personal and various usages. The data present in the worksheet is a combination of numbers and alphabets. If the worksheet contains more...

3 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.

Excel RIGHT() Function

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

1 minute read.

Square Root Function in Excel

The concept of Mathematical calculations includes a large number of numbers and calculations are based on the numbers present in the data. Likewise, Square Root is a function used to...

3 minutes read.