The VBA TimeSerial Function

Related Function:
VBA DateSerial

Description

The VBA TimeSerial Function returns a Time from a supplied hour, minute and second.

The syntax of the function is:

TimeSerial( Hour, Minute, Second )

Where the function arguments are:

 Hour - An integer (generally from 0 to 23) representing the hour of the required time. Minute - An integer (generally from 0 to 59) representing the minute of the required time.If the supplied Minute argument is less than 0 or greater than 59, this value is subtracted from, or added to, the supplied number of hours (as a partial hour). Second - An integer (generally from 0 to 59) representing the second of the required time.If the supplied Second argument is less than 0 or greater than 59, this value is subtracted from, or added to, the supplied number of minutes (as a partial minute).

VBA TimeSerial Function Examples

Example 1 - Simple Use of the TimeSerial Function

 ' Return two simple timesDim time1 As DateDim time2 As Datetime1 = TimeSerial( 11, 0, 0 )' The variable time1 is now equal to 11:00:00 AM.time2 = TimeSerial( 19, 15, 30 )' The variable time2 is now equal to 7:15:30 PM.

After running the above VBA code, the variable time1 = 11:00:00 AM and the variable time2 = 7:15:30 PM.

Example 2 - Using Minute or Second Numbers That Are Less Than 0 Or Greater Than 59

The following VBA code shows examples of the VBA TimeSerial function when the supplied Minute or Second argument is outside the range 0 - 59.

 ' Return four different times (minute or second is outside the range 0-59)Dim time1 As Datetime1 = TimeSerial( 8, -1, 0 )' The variable time1 is now equal to 7:59:00 AM.Dim time2 As Datetime2 = TimeSerial( 8, 60, 0 )' The variable time2 is now equal to 9:00:00 AM.Dim time3 As Datetime3 = TimeSerial( 8, 0, -1 )' The variable time3 is now equal to 7:59:59 AM.Dim time4 As Datetime4 = TimeSerial( 8, 0, 60 )' The variable time4 is now equal to 8:01:00 AM.