EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 120+ Courses All in One Bundle
  • Login
Home VBA VBA Resources VBA Date & Time Functions VBA Time
Secondary Sidebar
VBA Date & Time Functions
  • VBA Date and Time
    • VBA Month
    • VBA DateSerial
    • VBA Date Format
    • VBA Time
    • VBA TIMER
    • VBA Date
    • VBA Format Number
    • VBA DateAdd
    • VBA DateValue
    • VBA DatePart
    • VBA DateDiff
    • VBA Format Date

VBA Time

By Madhuri ThakurMadhuri Thakur

VBA Time

Excel VBA Time

VBA TIME function returns the current time as output and can also be used as an input to different macro lines of code. This function does not have any parameter set for it and thus, by far has the simplest syntax as TIME word with empty parentheses which can be seen as below:

The syntax for VBA TIME function:

Time()

In fact, there is no need for parentheses as well, you can simply use the word TIME itself in your block of code which allows your system to return the current time. We also have a similar function called NOW() under Excel VBA which returns the current time along with the date. However, in this article, our entire focus would be on using TIME function and looking at different working examples of the same.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

All in One Financial Analyst Bundle(250+ Courses, 40+ Projects)
Financial ModelingInvestment BankingUS GAAPCFA-Level 1 & 2
Equity ResearchM & A ModelingPrivate Equity ModelingForex Trading
Price
View Courses
250+ Online Courses | 40+ Projects | 1000+ Hours | Verifiable Certificates | Lifetime Access
4.9 (86,522 ratings)

How to Use Excel VBA Time Function?

We will learn how to use a VBA Time function with few examples in excel.

You can download this VBA Time Excel Template here – VBA Time Excel Template

VBA Time Function – Example #1

Current System Time Using TIME function:

Follow the below steps to display the current time of your system.

Step 1: Define a sub-procedure.

Code:

Sub TimeExample1()

End Sub

VBA Time Example 1-1

Step 2: Define a variable which will be useful to hold the value of time for you. Define it as String.

Code:

Sub TimeExample1()
Dim CurrentTime As String
End Sub

VBA Time Example 1-2

Note: The reason behind defining this variable as String is, the output of the TIME function is in a string.

Step 3: Use VBA TIME function to assign current time to the variable defined in the step above using an assignment operator.

Code:

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

VBA Time Example 1-3

Step 4: As I mentioned earlier, there is no need to add parentheses after TIME function as this is a volatile function. Having said that, the following code will give the equivalent output in comparison with the above code.

Code:

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

VBA Time Example 1-4

Step 5: Now, use a MsgBox function to display the current time using the display message box.

Code:

Sub TimeExample1()
Dim CurrentTime As String
CurrentTime = Time()
MsgBox CurrentTime
End Sub

VBA Time Example 1-5

Step 6: Run this code by hitting F5 or Run button and see the output. You should get the output as shown in the image below.

Result of Example 1-6

Please note that the time reflecting in the image above is the time by which this code is run on the system. While trying it by yourself, your output may vary depending on the time you are working on this code.

VBA Time Function – Example #2

VBA TIME in combination with a Date function to return the current time with Today’s Date:

As it has been informed earlier in the introduction of this article, there is a function called NOW() which gives you current date as well as time in Microsoft Excel. See the screenshot given below.

VBA Time Example 2-1

However, in VBA we don’t have this function working. Therefore, in VBA we have to use a combination of VBA Date and VBA TIME functions to get the current date and current time. VBA Date will give a current date and VBA Time will give current time (which we already have seen in the 1st example).

Let’s see step by step how we can achieve this.

Step 1: Insert a new module in your Excel VBA script.

VBA Time Example 2-2

Step 2: Define a new sub-procedure by giving a name to the macro.

Code: 

Sub Example2()

End Sub

VBA Time Example 2-3

Step 3: We wanted our output to be stored in cell A1 of our excel sheet. For that, start writing the code as below.

Code: 

Sub Example2()
Range("A1").Value =
End Sub

VBA Time Example 2-4

Step 4: Now, use a combination of VBA DATE and VBA TIME functions as shown below and assign it’s value to cell A1.

Code:

Sub Example2()
Range("A1").Value = Date & " " & Time
End Sub

VBA Time Example 2-5

The DATE function will return the current date of system and TIME function will return the current time. The white space enclosed in double quotes (“ “) allows having a space between date and time in the final output. Two & (and) operators work as concatenating operators which concatenate DATE and TIME with space in it.

Step 5: Run the code by hitting F5 or Run button manually. You will see the output in cell A1 of your worksheet as blow:

VBA Time Example 2-6

Here, again please note that the time reflecting in cell A1 is date and time by which this code is being run by myself. Therefore, when you’ll run this code the outputs will be different.

We also can format the date and time values using NumberFormat function under VBA.

Step 6: Write the code mentioned below in your VBA script.

Code:

Sub Example2()
Range("A1").Value = Date & " " & Time
Range("A1").NumberFormat = "dd-mmm-yyyy hh:mm:ss AM/PM"
End Sub

VBA Time Example 2-7

Step 7: Hit F5 or Run button manually and you’ll see the output as below in cell A1 of your worksheet.

Result of Example 2-8

VBA Time Function – Example #3

VBA TIME function to track the opening time and date of a workbook:

Sometimes, there is one spreadsheet which we open frequently (Like Employee list of an organization) and make changes into it. We might be interested in knowing the date and time on which the changes have been made in the worksheet. VBA Time function can be used to track the date and time every time a workbook is opened. See the steps below to work on the same.

Step 1: Create a new worksheet in your Excel workbook and rename it as per your convenience. I will rename it as (Time_Track).

Example 3-1

Step 2: Double click on ThisWorkbook under VBE (Visual Basics Editor).

This Workbook

Step 3: From object drop-down list select Workbook instead of General.

Workbook

Step 4: You’ll see a new private sub-procedure will get defined under the name Workbook_Open() in VBE.

Code:

Private Sub Workbook_Open()

End Sub

VBA Time Example 3-4

Step 5: Write down the below-mentioned code in this macro sub-procedure as shown in the image below:

Code:

Private Sub Workbook_Open()
Dim LB As Long
LB = Sheets("Time_Track").Cells(Rows.Count, 1).End(xlUp).Row + 1
Sheets("Time_Track").Cells(LB, 1).Value = Date & " " & Time()
End Sub

VBA Time Example 3-5

Here a variable LB of type Long is defined to find out the cell next to last used cell where the output can be stored. Then, using a combination of VBA DATE and TIME functions, we have assigned a value under that cell.

Step 6: Run the code by hitting F5 or Run button manually and see the output under column A of a worksheet.

Result of Example 3-6

This code will store the value of Date and Time every time you open the worksheet Time_Track.

This is it from this article. Let’s wrap the things up by writing some things to remember.

Things to Remember

  • VBA TIME function stores output as a string in excel. However, we also can store it as Date format while defining a variable as Date instead of a string.
  • TIME function does not require any argument in it. It’s a volatile function.
  • Even it does not need parentheses to call the function. Only TIME is enough to get the current time.
  • TIME function by default stores time value in hh:mm:ss format (24 hours clock format). However, sometimes the output format depends on your system time format as well.

Recommended Articles

This has been a guide to VBA Time Function. Here we have discussed how to use Excel VBA Time Functions and also we learned the Combination of date and time as an alternative to Now() Function and tracks the workbook open records along with some practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. VBA MsgBox
  2. VBA Do While Loop
  3. VBA IsError
  4. VBA XML
2 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign In
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Excel Charts
  • Excel Tips
  • All Tutorials
Certification Courses
  • All Courses
  • Excel VBA Course - All in One Bundle
  • VBA Course
  • Excel Data Analysis Course
  • Excel for Marketing Course
  • Excel for Finance Course
  • Excel for HR Training

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more