VBA TIMEVALUE Function

VBA TIMEVALUE Function

Objective of the VBA TimeValue Function :

The VBA TimeValue Function is a built-in function in MS Excel. It has one input argument or parameter. It returns the time value representing time for a input string value. It is a ‘Data and Time’ type function. It 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. In 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.

Table of Contents:

Syntax of VBA TimeValue Function

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

Here is 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

Instructions:

Here are the instructions to use the TimeValue Function in Excel

  1. Open an Excel workbook.
  2. Press Alt+F11 to open VBA Editor window.
  3. Go to Insert menu and click on module from the available options.
  4. Insert above specified macro example.
  5. Select code and click on ‘Run’ command or use Keyboard shortcut ‘F5’.
  6. Ex1-Output: It displays time on the screen.
  7. Ex2-Output: It displays time on the Worksheet at Rage B20.

Download File:

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

List of VBA Functions:

Please click on below link to go back to see all List of VBA Functions.
List of VBA Functions

Leave a Comment

Your email address will not be published. Required fields are marked *