VBA TIMEVALUE Function

VBA TIMEVALUE Function in Excel

VBA TimeValue Function in Excel is a built-in function in MS Excel. It has one input argument or parameter. VBA TimeValue Function returns the time value representing time for a input string value. It is a ‘Data and Time’ type function. TimeValue function can be used in either procedure or function in a VBA editor window in Excel.

We can use the VBA TimeValue Function any number of times in any number of procedures or functions. The following section we learn what is the syntax and parameters of the TimeValue function. We will also learn where we can use this VBA TimeValue Function and real-time examples.

VBA TimeValue Function Syntax

The syntax of the VBA TimeValue Function is

TIMEVALUE(time_value)

Parameters or Arguments

There is one parameter or argument for the TimeValue Function in VBA. That is nothing but ‘time_value’ parameter. It is a mandatory argument. It represents time in string format.

Where we can apply or use the VBA TimeValue Function?

We can use this VBA TimeValue Function in MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007 and Excel 2003. We can also use in 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.

Example 1: Convert the given input string to a valid time and display on the screen

Here is a simple example of the TimeValue Function. This below example macro uses the TimeValue Function and displays time for the specified string. In this case specified string represents date.

Sub VBA_TIMEVALUE_Function_Example1()
'Convert the given input string to a valid time and display on the screen
    
    'Variable declaration
    Dim CurrentTime As Date
    
    'Assign time to a variable
    CurrentTime = TimeValue("18:56:45")
    
    'Display output on the screen
    MsgBox "Current Time is: " & CurrentTime, vbInformation, "VBA TIMEVALUE Function"
    
End Sub 

In the above example ‘CurrentTime declared as Date data type. And TIMEVALUE is a vba function. This variable ‘CurrentTime’ now contains the time.
Output: Here is the screenshot of first example output.
VBA TIMEVALUE Function Example 1

Example 2: Convert the given input string to a valid time and display on the Worksheet

Let us see another example of the TimeValue Function. This below example macro uses the TimeValue Function and displays time for the specified string(date). Now, it displays on the Worksheet named ‘VBAF1.Com’ at Range ‘B20’.

Sub VBA_TIMEVALUE_Function_Example2()
'Convert the given input string to a valid time and display on the Worksheet
    
    'Variable declaration
    Dim CurrentTime As Date
    
    ‘Assign time to a variable
    CurrentTime = TimeValue("18:56:45")
    
    'Display output on the screen
    Sheets("VBAF1.COM").Range("B20") = "Current Time is: " & CurrentTime
    
End Sub

Output: Here is the screenshot of second example output.
VBA TIMEVALUE Function Example 2

Download File:

Click on the following link to download free example excel workbook to learn more about the TimeValue Function.

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

Leave a Reply