VBA HOUR Function (Syntax + Example)

Last Updated: June 22, 2023
puneet-gogia-excel-champs

- Written by Puneet

The VBA HOUR function is listed under the time category of VBA functions. When you use it in a VBA code, it returns the hour value from the time you supplied. As you know a valid VBA time is made up of hours, minutes, and seconds so it takes hours and ignores the rest of the part. It’s just like the HOUR function in the worksheet.

Syntax

Hour(Time)

Arguments

  • Time: A valid time as per VBA from which you want to get the hour value.

Example

To practically understand how to use VBA HOUR function, you need to go through the below example where we have written a vba code by usingit:

example-vba-hour-function
Sub example_HOUR()
Range("B1").Value = Hour(Range("A1"))
End Sub

In the above code, we have used HOUR to get the hour value from the time we have in cell A1 and it has returned 10 in cell B1 as the time we have in cell A1 is 10:34:45 AM.

Notes

  • If the value specified is a value other than a valid time or a string that can’t be recognized as a valid time, VBA will return the run-time 13 error.
  • If TIME is NULL, it will return NULL in the result.