TimeValue VBA Function – How to convert text to time

Last Updated on January 11, 2023 by token

1. VBA TimeValue Function – Description

The TimeValue VBA function  converts the time stored in text form into a formatted time with the date data type in VBA. This function is also used to extract time values ​​from date and time values. If we would like to convert a text date into a serial date, we should use the VBA DateValue function .

2. VBA TimeValue function – Syntax

TimeValue (Time As String)

Time : Time saved as text, e.g. “1: 5:15”, or # 12/28/2017 11:15:55 AM #

Function return: Date / Serial time.

3. VBA TimeValue function – Example

How to use TimeValue function in VBA Excel? Below is an example of using the TimeValue function in the VisualBasic Editor.

Example 1 : TimeValue VBA – text on time. The result in the MsgBox window and in the sheet cell.

Dim datVal As Date
datVal = TimeValue ("1:23:59")
MsgBox datVal
Range ("A1") = TimeValue ("1: 9: 59")

Example 2 : VBA TimeValue – extracting time from date and time.

Dim datVal2 As Date
datVal2 = # 12/28/2017 11:15:55 AM #
MsgBox TimeValue (datVal2)
Range ("A3") = TimeValue (datVal2)
Range ("A4") = TimeValue (Now ())
Excel VBA Functions - TimeValue VBA function
Excel VBA Functions – TimeValue VBA function

4. VB TimeValue Function – Additional information

  • The function can take different time or date and time formats as an argument, e.g. “1: 5:15”, or # 12/28/2017 11:15:55 AM #.

5. VisualBasic TimeValue function – Where to use?

The function can be used in: Excel 2003, Excel 2007, Excel 2010, Excel 2013, Excel 2016, Excel 2019, Excel 2021, Excel 365.

The article is part of the VBA Excel function list. You can find a list of all VBA functions at this address: VBA functions .

Leave a comment

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

%d bloggers like this: