VBA TimeSerial Function
The TimeSerial function in VBA returns a Time for the specified hour, minute, and second.
Syntax
TimeSerial (Hour, Minute, Second)
Parameter
Hour (required) – This parameter represents an integer (0 to 23), signifying the hour of the time.
Minute (required) – This parameter represents an integer (0 to 59), signifying the minute of the time. If it is less than 0 or greater than 59, this value is subtracted from, or added to, the specified number of hours.
Second (required) – This parameter represents an integer (0 to 59) representing the second of the time. If it is less than 0 or greater than 59, this value is subtracted from, or added to, the specified number of minutes.
Return
This function returns a Time for the given hour, minute, and second.
Example 1
Sub TimeSerialFunction_Example1() ' It will return the time dor given hour, minute, second Dim time_val As Date Dim hour_val As Integer, minute_val As Integer, second_val As Integer hour_val = 9 minute_val = 56 second_val = 45 'calculating the time based on above values time_val = TimeSerial(hour_val, minute_val, second_val) ' The variable time_val will rteurn a time value 9:56:45 AM. Cells(1, 1).Value = time_val End Sub
Output
9:56:45 AM
Example 2
Sub TimeSerialFunction_Example2() ' It will return the time dor given hour, minute, second Dim time_val As Date Dim hour_val As Integer, minute_val As Integer, second_val As Integer hour_val = 9 'when the supplied Minute or Second argument is outside the range 0 - 59. minute_val = -1 second_val = 45 'calculating the time based on above values time_val = TimeSerial(hour_val, minute_val, second_val) ' The variable time_val will rteurn a time value 9:56:45 AM. Cells(1, 1).Value = time_val End Sub
Output
8:59:45 AM
Example 3
Sub TimeSerialFunction_Example3() ' It will return the time dor given hour, minute, second Dim time_val As Date Dim hour_val As Integer, minute_val As Integer, second_val As Integer hour_val = 9 minute_val = 0 second_val = 60 'calculating the time based on above values time_val = TimeSerial(hour_val, minute_val, second_val) ' The variable time_val will rteurn a time value 9:01:00 AM. Cells(1, 1).Value = time_val End Sub
Output
9:01:00 AM