VBA Projects

Full Access with Source Code
  • Designed and Developed by PNRao

  • Full Access with VBA Source Code

  • Well Commented Codes Lines

  • Creative and Professional Design

Manage Your Projects

120+ Project Management Templates

Seamlessly manage your projects with our powerful & multi-purpose templates for project management.

120+ PM Templates Includes:
  • 50+ Excel Templates

  • 50+ PowerPoint Templates

  • 25+ Word Templates

Share Post

The Visual Basic for Applications (VBA) TimeSerial function is used to create a time value based on the specified hour, minute, and second parameters. It allows the user to easily manipulate and format time values within an Excel spreadsheet or VBA project.

VBA TimeSerial Function – Purpose, Syntax and Arguments


The TimeSerial function is useful for calculating time differences, setting specific times in a workbook, or converting strings to time values. It eliminates the need for complicated manual calculations and allows for more efficient coding.


TimeSerial(hour, minute, second)


  • hour: The desired hour value (0-23).
  • minute: The desired minute value (0-59).
  • second: The desired second value (0-59).


Assuming we have a cell containing the time value “4:30:10 PM”, we can use the TimeSerial function to add 2 hours, 10 minutes, and 20 seconds to this value.

Dim newTime As Date
newTime = TimeSerial(4, 30, 10) + TimeSerial(2, 10, 20)

The newTime variable will now hold the value “6:40:30 PM”.


  • The TimeSerial function returns a date value, which can also be formatted as a time value.
  • If any of the arguments are fractions, they will be truncated to the nearest whole number.
  • If the hour, minute, or second values are outside of their specified ranges, an error will be returned.

Important Notes:

  • Keep in mind that the TimeSerial function only works with time values, not date values. If you need to create a date and time value, you can use the ‘DateSerial’ function in combination with TimeSerial.
  • It’s important to properly format the output of the TimeSerial function as a time value using VBA’s built-in NumberFormat function or the Format function in Excel.
  • When using TimeSerial in a VBA project, it’s important to declare variables as a Date data type to avoid potential type mismatch errors.

Understanding VBA TimeSerial Function with Examples

Example 1: Basic Time Calculation

Dim startTime As Date
Dim endTime As Date
startTime = TimeSerial(8, 30, 0) '8:30 AM
endTime = TimeSerial(10, 45, 0)  '10:45 AM
MsgBox "The total duration is " & Format(endTime - startTime, "hh:mm") '2 hours and 15 minutes
  1. The first two lines declare two variables, startTime and endTime, of type Date. These variables will hold the starting and ending times for our calculation.
  2. The next two lines use the TimeSerial function to set the values for the startTime and endTime variables. The TimeSerial function takes in three arguments: hours, minutes, and seconds. In this example, we’re passing in the values 8, 30, and 0 to the TimeSerial function for startTime, which results in 8:30 AM. Similarly, 10, 45, and 0 are passed in for endTime, resulting in 10:45 AM.
  3. The MsgBox function is then used to display the result of the calculation. The Format function is used to format the result in hours and minutes format using the “hh:mm” argument. This results in the message box displaying “The total duration is 02:15” indicating that the duration between the two times is 2 hours and 15 minutes.

The TimeSerial function is a very useful tool when dealing with time calculations in VBA. It takes in the three arguments and returns a valid date value that can be used in calculations. This example showcases how simple it is to use the TimeSerial function to calculate the duration between two times.

Example 2: Time Addition

Dim startTime As Date
Dim duration As Date
startTime = TimeSerial(9, 0, 0)  '9:00 AM
duration = TimeSerial(1, 15, 0)  '1 hour and 15 minutes
MsgBox "The new time is " & Format(startTime + duration, "hh:mm") '10:15 AM
  1. In this example, we have a starting time of 9:00 AM and a duration of 1 hour and 15 minutes. Both these values are set using the TimeSerial function, with the starting time being in the variable startTime and the duration in the variable duration.
  2. The calculation is done by adding the two variables, startTime and duration, using the “+” operator. This results in a new time value of 10:15 AM.
  3. Finally, the result is displayed using the MsgBox function, with the Format function used to format the result in hours and minutes format.

The TimeSerial function also allows us to add or subtract time values from a given time. This example showcases this by adding a duration to an existing time value.

Example 3: Time Conversion

Dim givenTime As Date
Dim newTime As Date
givenTime = #1/1/2020 3:30:00 PM#  'given time in 24-hour format
newTime = TimeSerial(Hour(givenTime), Minute(givenTime), Second(givenTime)) '3:30 PM
MsgBox "The time is " & Format(newTime, "hh:mm AM/PM") '03:30 PM
  1. In this example, we have a given time value in 24-hour format (3:30 PM = 15:30).
  2. We use the TimeSerial function to convert this 24-hour format time value into a 12-hour format time value. This is achieved by using the Hour, Minute, and Second functions to extract the hours, minutes, and seconds from the givenTime variable and passing them as arguments to the TimeSerial function.
  3. The result is displayed using the MsgBox function, with the Format function used to format the time value in 12-hour format and include the AM/PM indicator.

This example showcases how the TimeSerial function can be used to convert time values from one format to another. In this case, from 24-hour format to 12-hour format.

Example 4: Time Validation

Dim validTime As Boolean
Dim time As Date
time = TimeSerial(13, 45, 0)  '1:45 PM
'validate if the given time is between 12 PM and 4 PM
If time > TimeSerial(12, 0, 0) And time < TimeSerial(16, 0, 0) Then
    validTime = True
    validTime = False
End If
If validTime Then
    MsgBox "The given time is valid."
    MsgBox "The given time is invalid."
End If
  1. In this example, we use the TimeSerial function to create a time value of 1:45 PM, which is stored in the variable time.
  2. A validation is then done to check if the given time falls between 12 PM and 4 PM. This is achieved by using the > and < operators with the TimeSerial function to create the time values for 12 PM and 4 PM.
  3. If the validation is successful, the variable validTime is set to True, or else it’s set to False.
  4. Finally, the result of the validation is displayed using the MsgBox function.

The TimeSerial function can also be used when validating time values. In this example, we check if a given time falls within a specific time period, and the TimeSerial function helps us create the time values for the start and end of the time period.

Effortlessly Manage Your Projects and Resources
120+ Professional Project Management Templates!

A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

Project Management Templates

All-in-One Pack
120+ Project Management Templates

Essential Pack
50+ PM Templates

Excel Pack
50+ Excel PM Templates

PowerPoint Pack
50+ Excel PM Templates

MS Word Pack
25+ Word PM Templates

Ultimate Project
Management Template
Ultimate Resource
Management Template
Project Portfolio
Management Templates
Categories: VBA FunctionsTags: , , , Last Updated: September 30, 2023

Leave A Comment