Excel VBA TimeValue Function

Excel VBA TimeValue Function: The TimeValue function in VBA returns a Time from the specified String interpretation of a time /date where the date information for the given string is ignored.

Syntax

TimeValue (Time)

Parameter

Time (required) – This parameter represents a valid String interpretation of time.

Return

This function returns a Time from the specified String interpretation of a time /date.

Example 1

Sub TimeValueFunction_Example1()
 ' Converting the given string into time
 Dim time_val As Date
 time_val = TimeValue("21:50:45")
 'The variable time_val will return the time 9:50:45 PM
 Cells(1, 1).Value = time_val
 End Sub 

Output

9:50:45 PM

VBA TimeValue Function

Example 2

Sub TimeValueFunction_Example2()
 ' Converting the given string into time
 Dim time_val As Date
 'will return the current Time
 time_val = TimeValue(Now())
 'The variable time_val will return the time 9:54:24 PM
 Cells(1, 1).Value = time_val
 End Sub 

Output

9:54:24 PM

VBA TimeValue Function

Example 3

Sub TimeValueFunction_Example3()
 ' Converting the given string into time
 Dim time_val As Date
 time_val = TimeValue("45:00")
 'The variable time_val will return type mismatch error
 Cells(1, 1).Value = time_val
 End Sub 

Output

VBA TimeValue Function