Excel VBA Time Function

Excel VBA Time Function: The Time function in VBA returns the current time.

Syntax

Time ()

Parameter

NA

Return

This function returns the current time. 

Example 1

Sub TimeFunction_Example1()
 ' returns the current time
 Dim time_val As Single
 time_val = Time()
 ' The variable time_val will return 0.876238406
 Cells(1, 1).Value = time_val
 End Sub 

Output

0.876238406

VBA Time Function

Example 2

Sub TimeFunction_Example2()
 ' returns the current time
 Dim time_val1 As Single, time_val2 As Single
 time_val1 = Time()
 MsgBox (time_val1 & " seconds")
 'returning the second time value
 time_val2 = Time() 
 MsgBox (time_val2 & " seconds")
 'returing the difference 
 MsgBox ("Time taken to run code:" & vbNewLine & time_val1 - time_val2 & " seconds")
 End Sub 

Output

VBA Time Function
VBA Time Function
VBA Time Function