In this video, we delve into various time functions in VBA (Visual Basic for Applications) that are essential for manipulating and displaying time data. We explore functions such as Now, Hour, Minute, Second, Time, Timer, TimeSerial, and TimeValue. The session includes practical examples demonstrating how these functions can be applied within VBA to achieve desired results.

What will you learn

  • How to use the Now function to capture the current date and time.
  • Extracting specific components like hours, minutes, and seconds from a given time using Hour, Minute, and Second functions.
  • Differences between the Now and Time functions.
  • How to calculate the elapsed time in seconds and milliseconds since midnight using the Timer function.
  • Constructing time values from individual hour, minute, and second components with TimeSerial.
  • Converting textual time representation into a proper time format using TimeValue.

Takeaway notes

  • The Now function returns the current date and time.
  • The Hour function extracts the hour component from a given time and operates in a 24-hour format.
  • The Minute and Second functions extract minute and second components respectively.
  • The Time function returns only the current time, without the date.
  • The Timer function calculates the number of seconds and milliseconds since midnight.
  • The TimeSerial function allows creating a time from given hour, minute, and second components.
  • The TimeValue function converts a text time representation into a date/time value.

Practice questions

  1. Write a VBA macro that displays the current date and time using the Now function.
  2. How can you extract just the hour component from the time "15:45:30"? Write the appropriate VBA code.
  3. Demonstrate the use of the Minute function to retrieve the minutes from the time "10:13:45".
  4. Provide an example of how to use the Second function to get the seconds from "23:59:55".
  5. Explain the differences between the Now and Time functions with examples.
  6. Create a macro using the Timer function to display the number of seconds and milliseconds since midnight.
  7. Write a VBA script to convert the text "13:45:30" into a time value using the TimeValue function.
  8. How would you use the TimeSerial function to create the time "08:30:25"? Provide a code snippet.
  9. Illustrate with code how to retrieve the current time without the date using VBA.
  10. Show an example where you use both Hour and Minute functions to split the time "14:33:22" into separate hour and minute variables.
  11. Write a VBA function that takes a time (e.g., "18:45:00") and returns it in a 12-hour format.
  12. Explain how to handle times written in a 24-hour format being returned in a 12-hour format by the Hour function.
  13. Create a subroutine that waits for a specified number of seconds using the Timer function.
  14. How can you use the TimeSerial function to add 1.5 hours to the given time "12:00:00"? Provide the solution.
  15. Craft a VBA script that extracts hour, minute, and second from the time "07:25:15" and displays them individually in a message box.

Mark Lesson Complete (Mastering Time Functions in VBA)