What is VBA TIMESERIAL Function
The VBA TIMESERIAL function is listed under the time category of VBA functions. When you use it in a VBA code, it returns a valid VBA time as per the hours, minutes, and seconds supplied. In simple words, you can create a time value with it supplying hours, minutes, and seconds.
how to use it
To use VBA's TIMESERIAL function you need to understand its syntax and arguments:
TimeSerial(Hour, Minute, Second)
- Hour: An integer ranging from 0 to 23 to specify the hours.
- Minute: An integer ranging from 0 to 59 to specify the minutes.
- Second: An integer ranging from 0 to 59 to specify the seconds.
Example to use TIMESERIAL Function in VBA
To practically understand how to use VBA TIMESERIAL function, you need to go through the below example where we have written a vba code by using it:
Sub example_TIMESERIAL() Range("A1").Value = TimeSerial(10, 35, 56) End Sub
In the above, we have used the TIMESERIAL to create valid time (as per VBA) by specifying the hours, minutes, and seconds in the function directly and it has returned that time in the cell A1.
Below are some important points which you need to take care while using TIMESERIAL function in VBA.
- TIMESERIAL can accept values outside the normal range. If you specify 61 minutes it will add 1 hour to hour value and use 1 as the minute value.
- If any argument is outside -32,768 to 32,767, VBA will return the run-time 13 error.
About the Author
Puneet is using Excel since his college days. He helped thousands of people to understand the power of the spreadsheets and learn Microsoft Excel. You can ﬁnd him online, tweeting about Excel, on a running track, or sometimes hiking up a mountain.