Excel Tutorial

Excel Tutorial Shortcut Keys in Excel Formatting in Excel Notes in Excel Formats in Excel Cells and Ranges in Excel Excel Function and Formulas Conditional Formatting in Excel Data Validation in Excel Charts in Excel Excel Ribbon Toolbar Basics of Excel Spell Check in Excel Data Analysis in Excel AutoFill in Excel Goal Seek in Excel Solver in Excel Pivots Table in Excel Go-To Special function in Excel Blank cells in Excel Count Cells with Text in Excel Date and Time in Excel-VBA Dependent Drop-down List in Excel Operators in Excel Dependent Combo box in Excel VBA Error Bar in Microsoft Excel Excel Axes Excel File using Password Excel Unique Values Frequency Distribution in Excel Gauge Chart in Excel Histogram in Excel Sum Every Nth Row in Microsoft Excel SumIF Formula in Microsoft Excel Multiplication in Excel Unique Values in Excel Trendline in Excel Excel Themes Copying formula in Excel Check Marks in Excel Calculating the Last Day of the Month in Excel Calculating Age in Excel Insert Row in Excel

Functions

Excel MAX() Function Excel INT() Function Excel MOD() Function Excel ROUND() Function Excel ROUNDUP() Function Excel AVERAGE() Function Excel COUNT() Function Excel COUNTA() Function Excel COUNTBLANK() Function Excel MIN() Function Excel EDATE() Function Excel EOMONTH() Function Excel HOUR() Function Excel MINUTE() Function Excel SECOND() Function Excel TIME() Function Excel WORKDAY() Function Excel WORKDAY.INTL() Function Excel DAYS() Function Excel WEEKNUM() Function Excel WEEKDAY() Function Excel SMALL() Function Excel LARGE() Function Excel LEFT() Function Excel RIGHT() Function Excel MID() Function Excel FIND() Function Excel SEARCH() Function Excel EXACT() Function Excel SUBSTITUTE() Function Excel TEXT() Function Excel VALUE() Function Excel AND() Function Excel OR() Function Excel IFERROR() Function Excel IF() Function Excel Nested IF’s Function Excel IFNA() Function Excel COUNTIFS() Function Excel VLOOKUP() Function Excel HLOOKUP() Function Excel INDEX() Function Excel MATCH() Function Excel OFFSET () Function Averageif Function in Excel

How To

How to import Microsoft Access data into the Microsoft Excel How to use TODAY function in Excel How to Alphabetize in Excel How to remove duplicate values from excel How to lock cells in Excel How to create drop down in excel How to Delete Row in Microsoft Excel How to Highlight Duplicates Words in the Microsoft Excel How to print titles in Excel How to make use of the Wildcard in Excel How to Make Use of the F-Test in Excel How to make use of the Excel Autofit in Excel How to generate random numbers in Excel How to apply Advanced Filter in Excel How to use Index and Match in Excel

Misc

Absolute Value in Excel Adding Column in Excel Converting Units in Excel Count Characters in Excel Custom Sort Order in Excel Decimals in Excel Division in Excel Locate Maximum Values in Excel Nearest Multiple in Excel Paste Options in Excel Quarter Dates in Excel Row Difference in Excel Separate Strings in Excel Reverse List in Excel Array Formula in Excel What if Analysis Data Table in Excel Excel Shortcut Keys What is a spreadsheet in Excel?

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.