VBA TIME Function

VBA TIME Function in Excel

VBA Time function is a built-in function in MS Excel. It does not take any input arguments or parameters. It returns the current system time. It is a ‘Data and Time’ type function. Default format of the Time function is ‘mm/dd/yyyy’. The VBA Time function can be used in either procedure or function in a VBA editor window in Excel. We can use the VBA time function any number of times in any number of procedures or functions. In the following section we learn what is the syntax and parameters of the time function, where we can use this VBA time function and real-time examples.

Syntax of VBA Time Function

The syntax of the VBA Time function is

Time()

In the above syntax parentheses is optional. If there is no argument, then no need to specify parentheses.

Parameters or Arguments

There are no parameters or arguments for the Time Function.

Where we can apply or use the VBA Time Function?

We can use this VBA date 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.

Display current system time on the screen

Here is a simple example of the VBA Time function. This below example macro uses the Time function and displays the current system time.

'Display Current System Time on the screen
Sub VBA_Time_Function_Ex1()
    
    'Variable declaration
    Dim sCurrentTime As Date
    
    'Assign current system time to variable
    sCurrentTime = Time
    MsgBox sCurrentTime, vbInformation, "Current System Time"
    
End Sub

In the above example ‘sCurrentTime’ declared as a Time data type. This variable ‘sCurrentTime’ now contains the current system time.
Output:
Here is the screen shot of first example output.
VBA TIME Function Example

 

Show current system time on the Worksheet

Here is another example of the VBA Time function. This below example macro uses the Time function and displays the current system time on the Worksheet named ‘VBAF1.Com’ in Range ‘B18’.

'Display Current System Time on the Worksheet
Sub VBA_Time_Function_Ex2()
    
    'Variable declaration
    Dim sCurrentTime As Date
    
    'Assign current system time to variable
    sCurrentTime = Time
    Sheets("Sheet1").Range("B18") = sCurrentTime
    'or
    'Sheets("Sheet1").Cells(18, 2) = sCurrentTime
    
End Sub

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

Change Time Format

Here is one more example with VBA Time function. This below example macro uses the Time function and changes the format of the time. Finally, it displays the current system time on the screen.

'Format and Display Current System Time on the screen
Sub VBA_Time_Function_Ex3()
    
    'Variable declaration
    Dim sCurrentTime As Date
    
    'Assign current system time to a
    sCurrentTime = Time
    MsgBox Format(sCurrentTime, "DD/MMMM/YYYY"), vbInformation, "Current System Time"
    
End Sub

Output:
Here is the screen shot of third example output.

VBA TIME Function Example 3

Download File

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

VBA Time 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

2 thoughts on “VBA TIME Function in Excel”

Leave a Reply